102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 3718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4718cf2ccSPedro F. Giffuni * 5b8b3f4fdSHans Petter Selasky * Copyright (c) 2008-2020 Hans Petter Selasky. All rights reserved. 602ac6454SAndrew Thompson * 702ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 802ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 902ac6454SAndrew Thompson * are met: 1002ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1202ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1302ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1402ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1502ac6454SAndrew Thompson * 1602ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1702ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1802ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1902ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2002ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2102ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2202ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2302ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2402ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2502ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2602ac6454SAndrew Thompson * SUCH DAMAGE. 2702ac6454SAndrew Thompson */ 2802ac6454SAndrew Thompson 29d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 30d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 31d2b99310SHans Petter Selasky #else 32ed6d949aSAndrew Thompson #include <sys/stdint.h> 33ed6d949aSAndrew Thompson #include <sys/stddef.h> 34ed6d949aSAndrew Thompson #include <sys/param.h> 35e2e050c8SConrad Meyer #include <sys/eventhandler.h> 36ed6d949aSAndrew Thompson #include <sys/queue.h> 37ed6d949aSAndrew Thompson #include <sys/systm.h> 38ed6d949aSAndrew Thompson #include <sys/kernel.h> 39ed6d949aSAndrew Thompson #include <sys/bus.h> 40ed6d949aSAndrew Thompson #include <sys/module.h> 41ed6d949aSAndrew Thompson #include <sys/lock.h> 42ed6d949aSAndrew Thompson #include <sys/mutex.h> 43ed6d949aSAndrew Thompson #include <sys/condvar.h> 44ed6d949aSAndrew Thompson #include <sys/sysctl.h> 45ed6d949aSAndrew Thompson #include <sys/sx.h> 46ed6d949aSAndrew Thompson #include <sys/unistd.h> 47ed6d949aSAndrew Thompson #include <sys/callout.h> 48ed6d949aSAndrew Thompson #include <sys/malloc.h> 49ed6d949aSAndrew Thompson #include <sys/priv.h> 50ed6d949aSAndrew Thompson #include <sys/conf.h> 51ed6d949aSAndrew Thompson #include <sys/fcntl.h> 52ed6d949aSAndrew Thompson 5302ac6454SAndrew Thompson #include <dev/usb/usb.h> 54ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 55ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 5602ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h> 57963169b4SHans Petter Selasky 58963169b4SHans Petter Selasky #if USB_HAVE_UGEN 59963169b4SHans Petter Selasky #include <sys/sbuf.h> 60963169b4SHans Petter Selasky #endif 61963169b4SHans Petter Selasky 6202ac6454SAndrew Thompson #include "usbdevs.h" 6302ac6454SAndrew Thompson 64a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_debug 6502ac6454SAndrew Thompson 6602ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 6702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 6802ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 6902ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 7002ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 7102ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h> 7202ac6454SAndrew Thompson #include <dev/usb/usb_request.h> 7302ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h> 7402ac6454SAndrew Thompson #include <dev/usb/usb_hub.h> 7502ac6454SAndrew Thompson #include <dev/usb/usb_util.h> 7602ac6454SAndrew Thompson #include <dev/usb/usb_msctest.h> 77bdc081c6SAndrew Thompson #if USB_HAVE_UGEN 78bdc081c6SAndrew Thompson #include <dev/usb/usb_dev.h> 7902ac6454SAndrew Thompson #include <dev/usb/usb_generic.h> 80bdc081c6SAndrew Thompson #endif 8102ac6454SAndrew Thompson 8202ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h> 8302ac6454SAndrew Thompson 8402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 8502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 86d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 8702ac6454SAndrew Thompson 8802ac6454SAndrew Thompson /* function prototypes */ 8902ac6454SAndrew Thompson 90f66c3cfcSEdward Tomasz Napierala static int sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS); 91a593f6b8SAndrew Thompson static void usb_init_endpoint(struct usb_device *, uint8_t, 92963169b4SHans Petter Selasky struct usb_endpoint_descriptor *, 93963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *, 94963169b4SHans Petter Selasky struct usb_endpoint *); 95a593f6b8SAndrew Thompson static void usb_unconfigure(struct usb_device *, uint8_t); 96a593f6b8SAndrew Thompson static void usb_detach_device_sub(struct usb_device *, device_t *, 978427ed84SHans Petter Selasky char **, uint8_t); 98a593f6b8SAndrew Thompson static uint8_t usb_probe_and_attach_sub(struct usb_device *, 99760bc48eSAndrew Thompson struct usb_attach_arg *); 100a593f6b8SAndrew Thompson static void usb_init_attach_arg(struct usb_device *, 101760bc48eSAndrew Thompson struct usb_attach_arg *); 102a593f6b8SAndrew Thompson static void usb_suspend_resume_sub(struct usb_device *, device_t, 10302ac6454SAndrew Thompson uint8_t); 104a0d53e0bSHans Petter Selasky static usb_proc_callback_t usbd_clear_stall_proc; 105963169b4SHans Petter Selasky static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t); 1068bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 107a593f6b8SAndrew Thompson static void usb_notify_addq(const char *type, struct usb_device *); 1088bb77249SHans Petter Selasky #endif 1098bb77249SHans Petter Selasky #if USB_HAVE_UGEN 110a593f6b8SAndrew Thompson static void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t); 111a593f6b8SAndrew Thompson static void usb_cdev_create(struct usb_device *); 112a593f6b8SAndrew Thompson static void usb_cdev_free(struct usb_device *); 1130f6e8f93SAndrew Thompson #endif 11402ac6454SAndrew Thompson 11502ac6454SAndrew Thompson /* This variable is global to allow easy access to it: */ 11602ac6454SAndrew Thompson 117fa49bce5SHans Petter Selasky #ifdef USB_TEMPLATE 118fa49bce5SHans Petter Selasky int usb_template = USB_TEMPLATE; 119fa49bce5SHans Petter Selasky #else 120c140287aSEdward Tomasz Napierala int usb_template = -1; 121fa49bce5SHans Petter Selasky #endif 12202ac6454SAndrew Thompson 123f66c3cfcSEdward Tomasz Napierala SYSCTL_PROC(_hw_usb, OID_AUTO, template, 124f66c3cfcSEdward Tomasz Napierala CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 125f66c3cfcSEdward Tomasz Napierala NULL, 0, sysctl_hw_usb_template, 126f66c3cfcSEdward Tomasz Napierala "I", "Selected USB device side template"); 127f66c3cfcSEdward Tomasz Napierala 128f66c3cfcSEdward Tomasz Napierala /*------------------------------------------------------------------------* 129f66c3cfcSEdward Tomasz Napierala * usb_trigger_reprobe_on_off 130f66c3cfcSEdward Tomasz Napierala * 131f66c3cfcSEdward Tomasz Napierala * This function sets the pull up resistors for all ports currently 132f66c3cfcSEdward Tomasz Napierala * operating in device mode either on (when on_not_off is 1), or off 133f66c3cfcSEdward Tomasz Napierala * (when it's 0). 134f66c3cfcSEdward Tomasz Napierala *------------------------------------------------------------------------*/ 135f66c3cfcSEdward Tomasz Napierala static void 136f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(int on_not_off) 137f66c3cfcSEdward Tomasz Napierala { 138f66c3cfcSEdward Tomasz Napierala struct usb_port_status ps; 139f66c3cfcSEdward Tomasz Napierala struct usb_bus *bus; 140f66c3cfcSEdward Tomasz Napierala struct usb_device *udev; 141f66c3cfcSEdward Tomasz Napierala usb_error_t err; 142f66c3cfcSEdward Tomasz Napierala int do_unlock, max; 143f66c3cfcSEdward Tomasz Napierala 144f66c3cfcSEdward Tomasz Napierala max = devclass_get_maxunit(usb_devclass_ptr); 145f66c3cfcSEdward Tomasz Napierala while (max >= 0) { 146f66c3cfcSEdward Tomasz Napierala mtx_lock(&usb_ref_lock); 147f66c3cfcSEdward Tomasz Napierala bus = devclass_get_softc(usb_devclass_ptr, max); 148f66c3cfcSEdward Tomasz Napierala max--; 149f66c3cfcSEdward Tomasz Napierala 150f66c3cfcSEdward Tomasz Napierala if (bus == NULL || bus->devices == NULL || 151f66c3cfcSEdward Tomasz Napierala bus->devices[USB_ROOT_HUB_ADDR] == NULL) { 152f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 153f66c3cfcSEdward Tomasz Napierala continue; 154f66c3cfcSEdward Tomasz Napierala } 155f66c3cfcSEdward Tomasz Napierala 156f66c3cfcSEdward Tomasz Napierala udev = bus->devices[USB_ROOT_HUB_ADDR]; 157f66c3cfcSEdward Tomasz Napierala 158f66c3cfcSEdward Tomasz Napierala if (udev->refcount == USB_DEV_REF_MAX) { 159f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 160f66c3cfcSEdward Tomasz Napierala continue; 161f66c3cfcSEdward Tomasz Napierala } 162f66c3cfcSEdward Tomasz Napierala 163f66c3cfcSEdward Tomasz Napierala udev->refcount++; 164f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 165f66c3cfcSEdward Tomasz Napierala 166f66c3cfcSEdward Tomasz Napierala do_unlock = usbd_enum_lock(udev); 167f66c3cfcSEdward Tomasz Napierala if (do_unlock > 1) { 168f66c3cfcSEdward Tomasz Napierala do_unlock = 0; 169f66c3cfcSEdward Tomasz Napierala goto next; 170f66c3cfcSEdward Tomasz Napierala } 171f66c3cfcSEdward Tomasz Napierala 172f66c3cfcSEdward Tomasz Napierala err = usbd_req_get_port_status(udev, NULL, &ps, 1); 173f66c3cfcSEdward Tomasz Napierala if (err != 0) { 174f66c3cfcSEdward Tomasz Napierala DPRINTF("usbd_req_get_port_status() " 175f66c3cfcSEdward Tomasz Napierala "failed: %s\n", usbd_errstr(err)); 176f66c3cfcSEdward Tomasz Napierala goto next; 177f66c3cfcSEdward Tomasz Napierala } 178f66c3cfcSEdward Tomasz Napierala 179f66c3cfcSEdward Tomasz Napierala if ((UGETW(ps.wPortStatus) & UPS_PORT_MODE_DEVICE) == 0) 180f66c3cfcSEdward Tomasz Napierala goto next; 181f66c3cfcSEdward Tomasz Napierala 182f66c3cfcSEdward Tomasz Napierala if (on_not_off) { 183f66c3cfcSEdward Tomasz Napierala err = usbd_req_set_port_feature(udev, NULL, 1, 184f66c3cfcSEdward Tomasz Napierala UHF_PORT_POWER); 185f66c3cfcSEdward Tomasz Napierala if (err != 0) { 186f66c3cfcSEdward Tomasz Napierala DPRINTF("usbd_req_set_port_feature() " 187f66c3cfcSEdward Tomasz Napierala "failed: %s\n", usbd_errstr(err)); 188f66c3cfcSEdward Tomasz Napierala } 189f66c3cfcSEdward Tomasz Napierala } else { 190f66c3cfcSEdward Tomasz Napierala err = usbd_req_clear_port_feature(udev, NULL, 1, 191f66c3cfcSEdward Tomasz Napierala UHF_PORT_POWER); 192f66c3cfcSEdward Tomasz Napierala if (err != 0) { 193f66c3cfcSEdward Tomasz Napierala DPRINTF("usbd_req_clear_port_feature() " 194f66c3cfcSEdward Tomasz Napierala "failed: %s\n", usbd_errstr(err)); 195f66c3cfcSEdward Tomasz Napierala } 196f66c3cfcSEdward Tomasz Napierala } 197f66c3cfcSEdward Tomasz Napierala 198f66c3cfcSEdward Tomasz Napierala next: 199f66c3cfcSEdward Tomasz Napierala mtx_lock(&usb_ref_lock); 200f66c3cfcSEdward Tomasz Napierala if (do_unlock) 201f66c3cfcSEdward Tomasz Napierala usbd_enum_unlock(udev); 202f66c3cfcSEdward Tomasz Napierala if (--(udev->refcount) == 0) 203f66c3cfcSEdward Tomasz Napierala cv_broadcast(&udev->ref_cv); 204f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 205f66c3cfcSEdward Tomasz Napierala } 206f66c3cfcSEdward Tomasz Napierala } 207f66c3cfcSEdward Tomasz Napierala 208f66c3cfcSEdward Tomasz Napierala /*------------------------------------------------------------------------* 209f66c3cfcSEdward Tomasz Napierala * usb_trigger_reprobe_all 210f66c3cfcSEdward Tomasz Napierala * 211f66c3cfcSEdward Tomasz Napierala * This function toggles the pull up resistors for all ports currently 212f66c3cfcSEdward Tomasz Napierala * operating in device mode, causing the host machine to reenumerate them. 213f66c3cfcSEdward Tomasz Napierala *------------------------------------------------------------------------*/ 214f66c3cfcSEdward Tomasz Napierala static void 215f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_all(void) 216f66c3cfcSEdward Tomasz Napierala { 217f66c3cfcSEdward Tomasz Napierala 218f66c3cfcSEdward Tomasz Napierala /* 219f66c3cfcSEdward Tomasz Napierala * Set the pull up resistors off for all ports in device mode. 220f66c3cfcSEdward Tomasz Napierala */ 221f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(0); 222f66c3cfcSEdward Tomasz Napierala 223f66c3cfcSEdward Tomasz Napierala /* 224f66c3cfcSEdward Tomasz Napierala * According to the DWC OTG spec this must be at least 3ms. 225f66c3cfcSEdward Tomasz Napierala */ 226f66c3cfcSEdward Tomasz Napierala usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME)); 227f66c3cfcSEdward Tomasz Napierala 228f66c3cfcSEdward Tomasz Napierala /* 229f66c3cfcSEdward Tomasz Napierala * Set the pull up resistors back on. 230f66c3cfcSEdward Tomasz Napierala */ 231f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(1); 232f66c3cfcSEdward Tomasz Napierala } 233f66c3cfcSEdward Tomasz Napierala 234f66c3cfcSEdward Tomasz Napierala static int 235f66c3cfcSEdward Tomasz Napierala sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS) 236f66c3cfcSEdward Tomasz Napierala { 237f66c3cfcSEdward Tomasz Napierala int error, val; 238f66c3cfcSEdward Tomasz Napierala 239f66c3cfcSEdward Tomasz Napierala val = usb_template; 240f66c3cfcSEdward Tomasz Napierala error = sysctl_handle_int(oidp, &val, 0, req); 241f66c3cfcSEdward Tomasz Napierala if (error != 0 || req->newptr == NULL || usb_template == val) 242f66c3cfcSEdward Tomasz Napierala return (error); 243f66c3cfcSEdward Tomasz Napierala 244f66c3cfcSEdward Tomasz Napierala usb_template = val; 245f66c3cfcSEdward Tomasz Napierala 246f66c3cfcSEdward Tomasz Napierala if (usb_template < 0) { 247f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(0); 248f66c3cfcSEdward Tomasz Napierala } else { 249f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_all(); 250f66c3cfcSEdward Tomasz Napierala } 251f66c3cfcSEdward Tomasz Napierala 252f66c3cfcSEdward Tomasz Napierala return (0); 253f66c3cfcSEdward Tomasz Napierala } 25402ac6454SAndrew Thompson 2557a918edfSAndrew Thompson /* English is default language */ 2567a918edfSAndrew Thompson 2577a918edfSAndrew Thompson static int usb_lang_id = 0x0009; 2587a918edfSAndrew Thompson static int usb_lang_mask = 0x00FF; 2597a918edfSAndrew Thompson 260af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RWTUN, 2617a918edfSAndrew Thompson &usb_lang_id, 0, "Preferred USB language ID"); 2627a918edfSAndrew Thompson 263af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RWTUN, 2647a918edfSAndrew Thompson &usb_lang_mask, 0, "Preferred USB language mask"); 2657a918edfSAndrew Thompson 2664d4fa6edSAndrew Thompson static const char* statestr[USB_STATE_MAX] = { 2674d4fa6edSAndrew Thompson [USB_STATE_DETACHED] = "DETACHED", 2684d4fa6edSAndrew Thompson [USB_STATE_ATTACHED] = "ATTACHED", 2694d4fa6edSAndrew Thompson [USB_STATE_POWERED] = "POWERED", 2704d4fa6edSAndrew Thompson [USB_STATE_ADDRESSED] = "ADDRESSED", 2714d4fa6edSAndrew Thompson [USB_STATE_CONFIGURED] = "CONFIGURED", 2724d4fa6edSAndrew Thompson }; 2734d4fa6edSAndrew Thompson 2744d4fa6edSAndrew Thompson const char * 275a593f6b8SAndrew Thompson usb_statestr(enum usb_dev_state state) 2764d4fa6edSAndrew Thompson { 277ec8f3127SAndrew Thompson return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN"); 2784d4fa6edSAndrew Thompson } 27902ac6454SAndrew Thompson 280ae538d85SAndrew Thompson const char * 281ae538d85SAndrew Thompson usb_get_manufacturer(struct usb_device *udev) 282ae538d85SAndrew Thompson { 283ae538d85SAndrew Thompson return (udev->manufacturer ? udev->manufacturer : "Unknown"); 284ae538d85SAndrew Thompson } 285ae538d85SAndrew Thompson 286ae538d85SAndrew Thompson const char * 287ae538d85SAndrew Thompson usb_get_product(struct usb_device *udev) 288ae538d85SAndrew Thompson { 289ae538d85SAndrew Thompson return (udev->product ? udev->product : ""); 290ae538d85SAndrew Thompson } 291ae538d85SAndrew Thompson 292ae538d85SAndrew Thompson const char * 293ae538d85SAndrew Thompson usb_get_serial(struct usb_device *udev) 294ae538d85SAndrew Thompson { 295ae538d85SAndrew Thompson return (udev->serial ? udev->serial : ""); 296ae538d85SAndrew Thompson } 297ae538d85SAndrew Thompson 29802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 299a593f6b8SAndrew Thompson * usbd_get_ep_by_addr 30002ac6454SAndrew Thompson * 301ae60fdfbSAndrew Thompson * This function searches for an USB ep by endpoint address and 30202ac6454SAndrew Thompson * direction. 30302ac6454SAndrew Thompson * 30402ac6454SAndrew Thompson * Returns: 30502ac6454SAndrew Thompson * NULL: Failure 30602ac6454SAndrew Thompson * Else: Success 30702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 308ae60fdfbSAndrew Thompson struct usb_endpoint * 309a593f6b8SAndrew Thompson usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val) 31002ac6454SAndrew Thompson { 311ae60fdfbSAndrew Thompson struct usb_endpoint *ep = udev->endpoints; 312ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 31302ac6454SAndrew Thompson enum { 31402ac6454SAndrew Thompson EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR), 31502ac6454SAndrew Thompson }; 31602ac6454SAndrew Thompson 31702ac6454SAndrew Thompson /* 31802ac6454SAndrew Thompson * According to the USB specification not all bits are used 31902ac6454SAndrew Thompson * for the endpoint address. Keep defined bits only: 32002ac6454SAndrew Thompson */ 32102ac6454SAndrew Thompson ea_val &= EA_MASK; 32202ac6454SAndrew Thompson 32302ac6454SAndrew Thompson /* 32420733245SPedro F. Giffuni * Iterate across all the USB endpoints searching for a match 32502ac6454SAndrew Thompson * based on the endpoint address: 32602ac6454SAndrew Thompson */ 327ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 328ae60fdfbSAndrew Thompson if (ep->edesc == NULL) { 32902ac6454SAndrew Thompson continue; 33002ac6454SAndrew Thompson } 33102ac6454SAndrew Thompson /* do the mask and check the value */ 332ae60fdfbSAndrew Thompson if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) { 33302ac6454SAndrew Thompson goto found; 33402ac6454SAndrew Thompson } 33502ac6454SAndrew Thompson } 33602ac6454SAndrew Thompson 33702ac6454SAndrew Thompson /* 338ae60fdfbSAndrew Thompson * The default endpoint is always present and is checked separately: 33902ac6454SAndrew Thompson */ 34022039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 3415b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 3425b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 34302ac6454SAndrew Thompson goto found; 34402ac6454SAndrew Thompson } 34502ac6454SAndrew Thompson return (NULL); 34602ac6454SAndrew Thompson 34702ac6454SAndrew Thompson found: 348ae60fdfbSAndrew Thompson return (ep); 34902ac6454SAndrew Thompson } 35002ac6454SAndrew Thompson 35102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 352a593f6b8SAndrew Thompson * usbd_get_endpoint 35302ac6454SAndrew Thompson * 354ae60fdfbSAndrew Thompson * This function searches for an USB endpoint based on the information 355760bc48eSAndrew Thompson * given by the passed "struct usb_config" pointer. 35602ac6454SAndrew Thompson * 35702ac6454SAndrew Thompson * Return values: 35802ac6454SAndrew Thompson * NULL: No match. 359ae60fdfbSAndrew Thompson * Else: Pointer to "struct usb_endpoint". 36002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 361ae60fdfbSAndrew Thompson struct usb_endpoint * 362a593f6b8SAndrew Thompson usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index, 363760bc48eSAndrew Thompson const struct usb_config *setup) 36402ac6454SAndrew Thompson { 365ae60fdfbSAndrew Thompson struct usb_endpoint *ep = udev->endpoints; 366ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 36702ac6454SAndrew Thompson uint8_t index = setup->ep_index; 36802ac6454SAndrew Thompson uint8_t ea_mask; 36902ac6454SAndrew Thompson uint8_t ea_val; 37002ac6454SAndrew Thompson uint8_t type_mask; 37102ac6454SAndrew Thompson uint8_t type_val; 37202ac6454SAndrew Thompson 37302ac6454SAndrew Thompson DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 37402ac6454SAndrew Thompson "type=0x%x dir=0x%x index=%d\n", 37502ac6454SAndrew Thompson udev, iface_index, setup->endpoint, 37602ac6454SAndrew Thompson setup->type, setup->direction, setup->ep_index); 37702ac6454SAndrew Thompson 3784eae601eSAndrew Thompson /* check USB mode */ 3794eae601eSAndrew Thompson 380f29a0724SAndrew Thompson if (setup->usb_mode != USB_MODE_DUAL && 381f29a0724SAndrew Thompson udev->flags.usb_mode != setup->usb_mode) { 382ae60fdfbSAndrew Thompson /* wrong mode - no endpoint */ 3834eae601eSAndrew Thompson return (NULL); 3844eae601eSAndrew Thompson } 3854eae601eSAndrew Thompson 38602ac6454SAndrew Thompson /* setup expected endpoint direction mask and value */ 38702ac6454SAndrew Thompson 38851ec1603SAndrew Thompson if (setup->direction == UE_DIR_RX) { 38951ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 390f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 39151ec1603SAndrew Thompson UE_DIR_OUT : UE_DIR_IN; 39251ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_TX) { 39351ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 394f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 39551ec1603SAndrew Thompson UE_DIR_IN : UE_DIR_OUT; 39651ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_ANY) { 39702ac6454SAndrew Thompson /* match any endpoint direction */ 39802ac6454SAndrew Thompson ea_mask = 0; 39902ac6454SAndrew Thompson ea_val = 0; 40002ac6454SAndrew Thompson } else { 40102ac6454SAndrew Thompson /* match the given endpoint direction */ 40202ac6454SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 40302ac6454SAndrew Thompson ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 40402ac6454SAndrew Thompson } 40502ac6454SAndrew Thompson 40602ac6454SAndrew Thompson /* setup expected endpoint address */ 40702ac6454SAndrew Thompson 40802ac6454SAndrew Thompson if (setup->endpoint == UE_ADDR_ANY) { 40902ac6454SAndrew Thompson /* match any endpoint address */ 41002ac6454SAndrew Thompson } else { 41102ac6454SAndrew Thompson /* match the given endpoint address */ 41202ac6454SAndrew Thompson ea_mask |= UE_ADDR; 41302ac6454SAndrew Thompson ea_val |= (setup->endpoint & UE_ADDR); 41402ac6454SAndrew Thompson } 41502ac6454SAndrew Thompson 41602ac6454SAndrew Thompson /* setup expected endpoint type */ 41702ac6454SAndrew Thompson 41802ac6454SAndrew Thompson if (setup->type == UE_BULK_INTR) { 41902ac6454SAndrew Thompson /* this will match BULK and INTERRUPT endpoints */ 42002ac6454SAndrew Thompson type_mask = 2; 42102ac6454SAndrew Thompson type_val = 2; 42202ac6454SAndrew Thompson } else if (setup->type == UE_TYPE_ANY) { 42302ac6454SAndrew Thompson /* match any endpoint type */ 42402ac6454SAndrew Thompson type_mask = 0; 42502ac6454SAndrew Thompson type_val = 0; 42602ac6454SAndrew Thompson } else { 42702ac6454SAndrew Thompson /* match the given endpoint type */ 42802ac6454SAndrew Thompson type_mask = UE_XFERTYPE; 42902ac6454SAndrew Thompson type_val = (setup->type & UE_XFERTYPE); 43002ac6454SAndrew Thompson } 43102ac6454SAndrew Thompson 43202ac6454SAndrew Thompson /* 43320733245SPedro F. Giffuni * Iterate across all the USB endpoints searching for a match 43402ac6454SAndrew Thompson * based on the endpoint address. Note that we are searching 435ae60fdfbSAndrew Thompson * the endpoints from the beginning of the "udev->endpoints" array. 43602ac6454SAndrew Thompson */ 437ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 438ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 439ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 44002ac6454SAndrew Thompson continue; 44102ac6454SAndrew Thompson } 44202ac6454SAndrew Thompson /* do the masks and check the values */ 44302ac6454SAndrew Thompson 444ae60fdfbSAndrew Thompson if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) && 445ae60fdfbSAndrew Thompson ((ep->edesc->bmAttributes & type_mask) == type_val)) { 44602ac6454SAndrew Thompson if (!index--) { 44702ac6454SAndrew Thompson goto found; 44802ac6454SAndrew Thompson } 44902ac6454SAndrew Thompson } 45002ac6454SAndrew Thompson } 45102ac6454SAndrew Thompson 45202ac6454SAndrew Thompson /* 453ae60fdfbSAndrew Thompson * Match against default endpoint last, so that "any endpoint", "any 454ae60fdfbSAndrew Thompson * address" and "any direction" returns the first endpoint of the 45502ac6454SAndrew Thompson * interface. "iface_index" and "direction" is ignored: 45602ac6454SAndrew Thompson */ 45722039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 4585b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) && 4595b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) && 46002ac6454SAndrew Thompson (!index)) { 4615b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 46202ac6454SAndrew Thompson goto found; 46302ac6454SAndrew Thompson } 46402ac6454SAndrew Thompson return (NULL); 46502ac6454SAndrew Thompson 46602ac6454SAndrew Thompson found: 467ae60fdfbSAndrew Thompson return (ep); 46802ac6454SAndrew Thompson } 46902ac6454SAndrew Thompson 47002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 471ed6d949aSAndrew Thompson * usbd_interface_count 47202ac6454SAndrew Thompson * 47302ac6454SAndrew Thompson * This function stores the number of USB interfaces excluding 47402ac6454SAndrew Thompson * alternate settings, which the USB config descriptor reports into 47502ac6454SAndrew Thompson * the unsigned 8-bit integer pointed to by "count". 47602ac6454SAndrew Thompson * 47702ac6454SAndrew Thompson * Returns: 47802ac6454SAndrew Thompson * 0: Success 47902ac6454SAndrew Thompson * Else: Failure 48002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 481e0a69b51SAndrew Thompson usb_error_t 482ed6d949aSAndrew Thompson usbd_interface_count(struct usb_device *udev, uint8_t *count) 48302ac6454SAndrew Thompson { 48402ac6454SAndrew Thompson if (udev->cdesc == NULL) { 48502ac6454SAndrew Thompson *count = 0; 48602ac6454SAndrew Thompson return (USB_ERR_NOT_CONFIGURED); 48702ac6454SAndrew Thompson } 488bdd41206SAndrew Thompson *count = udev->ifaces_max; 48902ac6454SAndrew Thompson return (USB_ERR_NORMAL_COMPLETION); 49002ac6454SAndrew Thompson } 49102ac6454SAndrew Thompson 49202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 493a593f6b8SAndrew Thompson * usb_init_endpoint 49402ac6454SAndrew Thompson * 495ae60fdfbSAndrew Thompson * This function will initialise the USB endpoint structure pointed to by 496ae60fdfbSAndrew Thompson * the "endpoint" argument. The structure pointed to by "endpoint" must be 497bdd41206SAndrew Thompson * zeroed before calling this function. 49802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 49902ac6454SAndrew Thompson static void 500a593f6b8SAndrew Thompson usb_init_endpoint(struct usb_device *udev, uint8_t iface_index, 501963169b4SHans Petter Selasky struct usb_endpoint_descriptor *edesc, 502963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *ecomp, 503963169b4SHans Petter Selasky struct usb_endpoint *ep) 50402ac6454SAndrew Thompson { 505e892b3feSHans Petter Selasky const struct usb_bus_methods *methods; 506a5cf1aaaSHans Petter Selasky usb_stream_t x; 507ee3e3ff5SAndrew Thompson 508bdd41206SAndrew Thompson methods = udev->bus->methods; 50902ac6454SAndrew Thompson 510ae60fdfbSAndrew Thompson (methods->endpoint_init) (udev, edesc, ep); 51102ac6454SAndrew Thompson 512ae60fdfbSAndrew Thompson /* initialise USB endpoint structure */ 513ae60fdfbSAndrew Thompson ep->edesc = edesc; 514963169b4SHans Petter Selasky ep->ecomp = ecomp; 515ae60fdfbSAndrew Thompson ep->iface_index = iface_index; 516a5cf1aaaSHans Petter Selasky 517a5cf1aaaSHans Petter Selasky /* setup USB stream queues */ 518a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 519a5cf1aaaSHans Petter Selasky TAILQ_INIT(&ep->endpoint_q[x].head); 520a5cf1aaaSHans Petter Selasky ep->endpoint_q[x].command = &usbd_pipe_start; 521a5cf1aaaSHans Petter Selasky } 52202ac6454SAndrew Thompson 52339307315SAndrew Thompson /* the pipe is not supported by the hardware */ 524ae60fdfbSAndrew Thompson if (ep->methods == NULL) 52539307315SAndrew Thompson return; 52639307315SAndrew Thompson 527a5cf1aaaSHans Petter Selasky /* check for SUPER-speed streams mode endpoint */ 528a5cf1aaaSHans Petter Selasky if (udev->speed == USB_SPEED_SUPER && ecomp != NULL && 529a5cf1aaaSHans Petter Selasky (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK && 530a5cf1aaaSHans Petter Selasky (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) { 531a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS); 532a5cf1aaaSHans Petter Selasky } else { 533a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT); 534a5cf1aaaSHans Petter Selasky } 535a5cf1aaaSHans Petter Selasky 53602ac6454SAndrew Thompson /* clear stall, if any */ 537bdd41206SAndrew Thompson if (methods->clear_stall != NULL) { 53802ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 539ae60fdfbSAndrew Thompson (methods->clear_stall) (udev, ep); 54002ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 54102ac6454SAndrew Thompson } 54202ac6454SAndrew Thompson } 54302ac6454SAndrew Thompson 544bdd41206SAndrew Thompson /*-----------------------------------------------------------------------* 545a593f6b8SAndrew Thompson * usb_endpoint_foreach 54616589beaSAndrew Thompson * 54716589beaSAndrew Thompson * This function will iterate all the USB endpoints except the control 54816589beaSAndrew Thompson * endpoint. This function is NULL safe. 54916589beaSAndrew Thompson * 55016589beaSAndrew Thompson * Return values: 551ae60fdfbSAndrew Thompson * NULL: End of USB endpoints 552ae60fdfbSAndrew Thompson * Else: Pointer to next USB endpoint 55316589beaSAndrew Thompson *------------------------------------------------------------------------*/ 554ae60fdfbSAndrew Thompson struct usb_endpoint * 555a593f6b8SAndrew Thompson usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep) 55616589beaSAndrew Thompson { 557a9e26757SAndrew Thompson struct usb_endpoint *ep_end; 55816589beaSAndrew Thompson 55916589beaSAndrew Thompson /* be NULL safe */ 56016589beaSAndrew Thompson if (udev == NULL) 56116589beaSAndrew Thompson return (NULL); 56216589beaSAndrew Thompson 563a9e26757SAndrew Thompson ep_end = udev->endpoints + udev->endpoints_max; 564a9e26757SAndrew Thompson 565ae60fdfbSAndrew Thompson /* get next endpoint */ 566ae60fdfbSAndrew Thompson if (ep == NULL) 567ae60fdfbSAndrew Thompson ep = udev->endpoints; 56816589beaSAndrew Thompson else 569ae60fdfbSAndrew Thompson ep++; 57016589beaSAndrew Thompson 571ae60fdfbSAndrew Thompson /* find next allocated ep */ 572ae60fdfbSAndrew Thompson while (ep != ep_end) { 573ae60fdfbSAndrew Thompson if (ep->edesc != NULL) 574ae60fdfbSAndrew Thompson return (ep); 575ae60fdfbSAndrew Thompson ep++; 57616589beaSAndrew Thompson } 57716589beaSAndrew Thompson return (NULL); 57816589beaSAndrew Thompson } 57916589beaSAndrew Thompson 58016589beaSAndrew Thompson /*------------------------------------------------------------------------* 581b78e84d1SHans Petter Selasky * usb_wait_pending_refs 582d008478eSHans Petter Selasky * 583d008478eSHans Petter Selasky * This function will wait for any USB references to go away before 584b78e84d1SHans Petter Selasky * returning. This function is used before freeing a USB device. 585d008478eSHans Petter Selasky *------------------------------------------------------------------------*/ 586d008478eSHans Petter Selasky static void 587b78e84d1SHans Petter Selasky usb_wait_pending_refs(struct usb_device *udev) 588d008478eSHans Petter Selasky { 589d008478eSHans Petter Selasky #if USB_HAVE_UGEN 590b78e84d1SHans Petter Selasky DPRINTF("Refcount = %d\n", (int)udev->refcount); 591d008478eSHans Petter Selasky 592b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 593b78e84d1SHans Petter Selasky udev->refcount--; 594d008478eSHans Petter Selasky while (1) { 595d008478eSHans Petter Selasky /* wait for any pending references to go away */ 596b78e84d1SHans Petter Selasky if (udev->refcount == 0) { 597b78e84d1SHans Petter Selasky /* prevent further refs being taken, if any */ 598d008478eSHans Petter Selasky udev->refcount = USB_DEV_REF_MAX; 599d008478eSHans Petter Selasky break; 600d008478eSHans Petter Selasky } 601d008478eSHans Petter Selasky cv_wait(&udev->ref_cv, &usb_ref_lock); 602d008478eSHans Petter Selasky } 603d008478eSHans Petter Selasky mtx_unlock(&usb_ref_lock); 604d008478eSHans Petter Selasky #endif 605d008478eSHans Petter Selasky } 606d008478eSHans Petter Selasky 607d008478eSHans Petter Selasky /*------------------------------------------------------------------------* 608a593f6b8SAndrew Thompson * usb_unconfigure 60902ac6454SAndrew Thompson * 610ae60fdfbSAndrew Thompson * This function will free all USB interfaces and USB endpoints belonging 61102ac6454SAndrew Thompson * to an USB device. 612bdd41206SAndrew Thompson * 613bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 61402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 61502ac6454SAndrew Thompson static void 616a593f6b8SAndrew Thompson usb_unconfigure(struct usb_device *udev, uint8_t flag) 61702ac6454SAndrew Thompson { 618bdd41206SAndrew Thompson uint8_t do_unlock; 61902ac6454SAndrew Thompson 6206950c75fSHans Petter Selasky /* Prevent re-enumeration */ 6216950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 622bdd41206SAndrew Thompson 623bdd41206SAndrew Thompson /* detach all interface drivers */ 624a593f6b8SAndrew Thompson usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag); 625bdd41206SAndrew Thompson 626bdd41206SAndrew Thompson #if USB_HAVE_UGEN 627bdd41206SAndrew Thompson /* free all FIFOs except control endpoint FIFOs */ 628a593f6b8SAndrew Thompson usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag); 629bdd41206SAndrew Thompson 630bdd41206SAndrew Thompson /* 631bdd41206SAndrew Thompson * Free all cdev's, if any. 632bdd41206SAndrew Thompson */ 633a593f6b8SAndrew Thompson usb_cdev_free(udev); 634bdd41206SAndrew Thompson #endif 63502ac6454SAndrew Thompson 636bdc081c6SAndrew Thompson #if USB_HAVE_COMPAT_LINUX 63702ac6454SAndrew Thompson /* free Linux compat device, if any */ 6388d59ecb2SHans Petter Selasky if (udev->linux_endpoint_start != NULL) { 6398d59ecb2SHans Petter Selasky usb_linux_free_device_p(udev); 640760bc48eSAndrew Thompson udev->linux_endpoint_start = NULL; 64102ac6454SAndrew Thompson } 642bdc081c6SAndrew Thompson #endif 64302ac6454SAndrew Thompson 644a593f6b8SAndrew Thompson usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE); 64502ac6454SAndrew Thompson 646ae60fdfbSAndrew Thompson /* free "cdesc" after "ifaces" and "endpoints", if any */ 6477efaaa9aSAndrew Thompson if (udev->cdesc != NULL) { 648f29a0724SAndrew Thompson if (udev->flags.usb_mode != USB_MODE_DEVICE) 6492c79a775SHans Petter Selasky usbd_free_config_desc(udev, udev->cdesc); 65002ac6454SAndrew Thompson udev->cdesc = NULL; 65102ac6454SAndrew Thompson } 65202ac6454SAndrew Thompson /* set unconfigured state */ 65302ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 65402ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 655bdd41206SAndrew Thompson 656cb18f7d1SAlfred Perlstein if (do_unlock) 657cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 65802ac6454SAndrew Thompson } 65902ac6454SAndrew Thompson 66002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 661a593f6b8SAndrew Thompson * usbd_set_config_index 66202ac6454SAndrew Thompson * 66302ac6454SAndrew Thompson * This function selects configuration by index, independent of the 66402ac6454SAndrew Thompson * actual configuration number. This function should not be used by 66502ac6454SAndrew Thompson * USB drivers. 66602ac6454SAndrew Thompson * 66702ac6454SAndrew Thompson * Returns: 66802ac6454SAndrew Thompson * 0: Success 66902ac6454SAndrew Thompson * Else: Failure 67002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 671e0a69b51SAndrew Thompson usb_error_t 672a593f6b8SAndrew Thompson usbd_set_config_index(struct usb_device *udev, uint8_t index) 67302ac6454SAndrew Thompson { 674760bc48eSAndrew Thompson struct usb_status ds; 675760bc48eSAndrew Thompson struct usb_config_descriptor *cdp; 67602ac6454SAndrew Thompson uint16_t power; 67702ac6454SAndrew Thompson uint16_t max_power; 67802ac6454SAndrew Thompson uint8_t selfpowered; 67902ac6454SAndrew Thompson uint8_t do_unlock; 680e0a69b51SAndrew Thompson usb_error_t err; 68102ac6454SAndrew Thompson 68202ac6454SAndrew Thompson DPRINTFN(6, "udev=%p index=%d\n", udev, index); 68302ac6454SAndrew Thompson 6846950c75fSHans Petter Selasky /* Prevent re-enumeration */ 6856950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 68602ac6454SAndrew Thompson 687d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 68802ac6454SAndrew Thompson 68902ac6454SAndrew Thompson if (index == USB_UNCONFIG_INDEX) { 69002ac6454SAndrew Thompson /* 69102ac6454SAndrew Thompson * Leave unallocated when unconfiguring the 692a593f6b8SAndrew Thompson * device. "usb_unconfigure()" will also reset 69302ac6454SAndrew Thompson * the current config number and index. 69402ac6454SAndrew Thompson */ 695a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO); 696bd216778SAndrew Thompson if (udev->state == USB_STATE_CONFIGURED) 697a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 69802ac6454SAndrew Thompson goto done; 69902ac6454SAndrew Thompson } 70002ac6454SAndrew Thompson /* get the full config descriptor */ 701f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 7027efaaa9aSAndrew Thompson /* save some memory */ 703a593f6b8SAndrew Thompson err = usbd_req_get_descriptor_ptr(udev, &cdp, 704459d369eSAndrew Thompson (UDESC_CONFIG << 8) | index); 7057efaaa9aSAndrew Thompson } else { 7067efaaa9aSAndrew Thompson /* normal request */ 707a593f6b8SAndrew Thompson err = usbd_req_get_config_desc_full(udev, 7082c79a775SHans Petter Selasky NULL, &cdp, index); 7097efaaa9aSAndrew Thompson } 71002ac6454SAndrew Thompson if (err) { 71102ac6454SAndrew Thompson goto done; 71202ac6454SAndrew Thompson } 71302ac6454SAndrew Thompson /* set the new config descriptor */ 71402ac6454SAndrew Thompson 71502ac6454SAndrew Thompson udev->cdesc = cdp; 71602ac6454SAndrew Thompson 71702ac6454SAndrew Thompson /* Figure out if the device is self or bus powered. */ 71802ac6454SAndrew Thompson selfpowered = 0; 71902ac6454SAndrew Thompson if ((!udev->flags.uq_bus_powered) && 72002ac6454SAndrew Thompson (cdp->bmAttributes & UC_SELF_POWERED) && 721f29a0724SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST)) { 72202ac6454SAndrew Thompson /* May be self powered. */ 72302ac6454SAndrew Thompson if (cdp->bmAttributes & UC_BUS_POWERED) { 72402ac6454SAndrew Thompson /* Must ask device. */ 725a593f6b8SAndrew Thompson err = usbd_req_get_device_status(udev, NULL, &ds); 72602ac6454SAndrew Thompson if (err) { 72702ac6454SAndrew Thompson DPRINTFN(0, "could not read " 72802ac6454SAndrew Thompson "device status: %s\n", 729a593f6b8SAndrew Thompson usbd_errstr(err)); 73002ac6454SAndrew Thompson } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 73102ac6454SAndrew Thompson selfpowered = 1; 73202ac6454SAndrew Thompson } 73302ac6454SAndrew Thompson DPRINTF("status=0x%04x \n", 73402ac6454SAndrew Thompson UGETW(ds.wStatus)); 73502ac6454SAndrew Thompson } else 73602ac6454SAndrew Thompson selfpowered = 1; 73702ac6454SAndrew Thompson } 73802ac6454SAndrew Thompson DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 73902ac6454SAndrew Thompson "selfpowered=%d, power=%d\n", 74002ac6454SAndrew Thompson udev, cdp, 74191f72cedSAndrew Thompson udev->address, cdp->bConfigurationValue, cdp->bmAttributes, 74202ac6454SAndrew Thompson selfpowered, cdp->bMaxPower * 2); 74302ac6454SAndrew Thompson 74402ac6454SAndrew Thompson /* Check if we have enough power. */ 74502ac6454SAndrew Thompson power = cdp->bMaxPower * 2; 74602ac6454SAndrew Thompson 74702ac6454SAndrew Thompson if (udev->parent_hub) { 74802ac6454SAndrew Thompson max_power = udev->parent_hub->hub->portpower; 74902ac6454SAndrew Thompson } else { 75002ac6454SAndrew Thompson max_power = USB_MAX_POWER; 75102ac6454SAndrew Thompson } 75202ac6454SAndrew Thompson 75302ac6454SAndrew Thompson if (power > max_power) { 75402ac6454SAndrew Thompson DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 75502ac6454SAndrew Thompson err = USB_ERR_NO_POWER; 75602ac6454SAndrew Thompson goto done; 75702ac6454SAndrew Thompson } 75802ac6454SAndrew Thompson /* Only update "self_powered" in USB Host Mode */ 759f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 76002ac6454SAndrew Thompson udev->flags.self_powered = selfpowered; 76102ac6454SAndrew Thompson } 76202ac6454SAndrew Thompson udev->power = power; 76302ac6454SAndrew Thompson udev->curr_config_no = cdp->bConfigurationValue; 76402ac6454SAndrew Thompson udev->curr_config_index = index; 765a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_CONFIGURED); 76602ac6454SAndrew Thompson 76702ac6454SAndrew Thompson /* Set the actual configuration value. */ 768a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue); 76902ac6454SAndrew Thompson if (err) { 77002ac6454SAndrew Thompson goto done; 77102ac6454SAndrew Thompson } 772bdd41206SAndrew Thompson 773a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC); 77402ac6454SAndrew Thompson if (err) { 77502ac6454SAndrew Thompson goto done; 77602ac6454SAndrew Thompson } 777bdd41206SAndrew Thompson 778a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT); 779bdd41206SAndrew Thompson if (err) { 780bdd41206SAndrew Thompson goto done; 78102ac6454SAndrew Thompson } 782bdd41206SAndrew Thompson 7830f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 784ee3e3ff5SAndrew Thompson /* create device nodes for each endpoint */ 785a593f6b8SAndrew Thompson usb_cdev_create(udev); 7860f6e8f93SAndrew Thompson #endif 78702ac6454SAndrew Thompson 78802ac6454SAndrew Thompson done: 789a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err)); 79002ac6454SAndrew Thompson if (err) { 791d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 79202ac6454SAndrew Thompson } 793cb18f7d1SAlfred Perlstein if (do_unlock) 794cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 79502ac6454SAndrew Thompson return (err); 79602ac6454SAndrew Thompson } 79702ac6454SAndrew Thompson 79802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 799a593f6b8SAndrew Thompson * usb_config_parse 800bdd41206SAndrew Thompson * 801ae60fdfbSAndrew Thompson * This function will allocate and free USB interfaces and USB endpoints, 802ae60fdfbSAndrew Thompson * parse the USB configuration structure and initialise the USB endpoints 803bdd41206SAndrew Thompson * and interfaces. If "iface_index" is not equal to 804bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the 805bdd41206SAndrew Thompson * alternate_setting to be selected for the given interface. Else the 806bdd41206SAndrew Thompson * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be 807bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function 808bdd41206SAndrew Thompson * is typically called when setting the configuration or when setting 809bdd41206SAndrew Thompson * an alternate interface. 810bdd41206SAndrew Thompson * 811bdd41206SAndrew Thompson * Returns: 812bdd41206SAndrew Thompson * 0: Success 813bdd41206SAndrew Thompson * Else: Failure 814bdd41206SAndrew Thompson *------------------------------------------------------------------------*/ 815963169b4SHans Petter Selasky static usb_error_t 816a593f6b8SAndrew Thompson usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd) 817bdd41206SAndrew Thompson { 818760bc48eSAndrew Thompson struct usb_idesc_parse_state ips; 819760bc48eSAndrew Thompson struct usb_interface_descriptor *id; 820760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 821760bc48eSAndrew Thompson struct usb_interface *iface; 822ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 823e0a69b51SAndrew Thompson usb_error_t err; 824bdd41206SAndrew Thompson uint8_t ep_curr; 825bdd41206SAndrew Thompson uint8_t ep_max; 826bdd41206SAndrew Thompson uint8_t temp; 827bdd41206SAndrew Thompson uint8_t do_init; 828bdd41206SAndrew Thompson uint8_t alt_index; 829bdd41206SAndrew Thompson 830bdd41206SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 831bdd41206SAndrew Thompson /* parameter overload */ 832bdd41206SAndrew Thompson alt_index = cmd; 833bdd41206SAndrew Thompson cmd = USB_CFG_INIT; 834bdd41206SAndrew Thompson } else { 835bdd41206SAndrew Thompson /* not used */ 836bdd41206SAndrew Thompson alt_index = 0; 837bdd41206SAndrew Thompson } 838bdd41206SAndrew Thompson 839bdd41206SAndrew Thompson err = 0; 840bdd41206SAndrew Thompson 841bdd41206SAndrew Thompson DPRINTFN(5, "iface_index=%d cmd=%d\n", 842bdd41206SAndrew Thompson iface_index, cmd); 843bdd41206SAndrew Thompson 844bdd41206SAndrew Thompson if (cmd == USB_CFG_FREE) 845bdd41206SAndrew Thompson goto cleanup; 846bdd41206SAndrew Thompson 847bdd41206SAndrew Thompson if (cmd == USB_CFG_INIT) { 84891cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 849bdd41206SAndrew Thompson 850ae60fdfbSAndrew Thompson /* check for in-use endpoints */ 851bdd41206SAndrew Thompson 852ae60fdfbSAndrew Thompson ep = udev->endpoints; 853ae60fdfbSAndrew Thompson ep_max = udev->endpoints_max; 854bdd41206SAndrew Thompson while (ep_max--) { 855ae60fdfbSAndrew Thompson /* look for matching endpoints */ 856bdd41206SAndrew Thompson if ((iface_index == USB_IFACE_INDEX_ANY) || 857ae60fdfbSAndrew Thompson (iface_index == ep->iface_index)) { 858f12c6c29SAndrew Thompson if (ep->refcount_alloc != 0) { 859bdd41206SAndrew Thompson /* 860bdd41206SAndrew Thompson * This typically indicates a 861bdd41206SAndrew Thompson * more serious error. 862bdd41206SAndrew Thompson */ 863bdd41206SAndrew Thompson err = USB_ERR_IN_USE; 864bdd41206SAndrew Thompson } else { 865ae60fdfbSAndrew Thompson /* reset endpoint */ 866ae60fdfbSAndrew Thompson memset(ep, 0, sizeof(*ep)); 867ae60fdfbSAndrew Thompson /* make sure we don't zero the endpoint again */ 868ae60fdfbSAndrew Thompson ep->iface_index = USB_IFACE_INDEX_ANY; 869bdd41206SAndrew Thompson } 870bdd41206SAndrew Thompson } 871ae60fdfbSAndrew Thompson ep++; 872bdd41206SAndrew Thompson } 873bdd41206SAndrew Thompson 874bdd41206SAndrew Thompson if (err) 875bdd41206SAndrew Thompson return (err); 876bdd41206SAndrew Thompson } 877bdd41206SAndrew Thompson 878bdd41206SAndrew Thompson memset(&ips, 0, sizeof(ips)); 879bdd41206SAndrew Thompson 880bdd41206SAndrew Thompson ep_curr = 0; 881bdd41206SAndrew Thompson ep_max = 0; 882bdd41206SAndrew Thompson 883a593f6b8SAndrew Thompson while ((id = usb_idesc_foreach(udev->cdesc, &ips))) { 884bdd41206SAndrew Thompson iface = udev->ifaces + ips.iface_index; 885bdd41206SAndrew Thompson 886bdd41206SAndrew Thompson /* check for specific interface match */ 887bdd41206SAndrew Thompson 888bdd41206SAndrew Thompson if (cmd == USB_CFG_INIT) { 889bdd41206SAndrew Thompson if ((iface_index != USB_IFACE_INDEX_ANY) && 890bdd41206SAndrew Thompson (iface_index != ips.iface_index)) { 891bdd41206SAndrew Thompson /* wrong interface */ 892bdd41206SAndrew Thompson do_init = 0; 893bdd41206SAndrew Thompson } else if (alt_index != ips.iface_index_alt) { 894bdd41206SAndrew Thompson /* wrong alternate setting */ 895bdd41206SAndrew Thompson do_init = 0; 896bdd41206SAndrew Thompson } else { 897bdd41206SAndrew Thompson /* initialise interface */ 898bdd41206SAndrew Thompson do_init = 1; 899bdd41206SAndrew Thompson } 900b1f99f9cSHans Petter Selasky /* update number of alternate settings, if any */ 901b1f99f9cSHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) 902b1f99f9cSHans Petter Selasky iface->num_altsetting = ips.iface_index_alt + 1; 903bdd41206SAndrew Thompson } else 904bdd41206SAndrew Thompson do_init = 0; 905bdd41206SAndrew Thompson 906bdd41206SAndrew Thompson /* check for new interface */ 907bdd41206SAndrew Thompson if (ips.iface_index_alt == 0) { 908bdd41206SAndrew Thompson /* update current number of endpoints */ 909bdd41206SAndrew Thompson ep_curr = ep_max; 910bdd41206SAndrew Thompson } 911b8b3f4fdSHans Petter Selasky 912bdd41206SAndrew Thompson /* check for init */ 913bdd41206SAndrew Thompson if (do_init) { 914bdd41206SAndrew Thompson /* setup the USB interface structure */ 915bdd41206SAndrew Thompson iface->idesc = id; 916bdd41206SAndrew Thompson /* set alternate index */ 917bdd41206SAndrew Thompson iface->alt_index = alt_index; 918d9073c1eSHans Petter Selasky /* set default interface parent */ 919d9073c1eSHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 920d9073c1eSHans Petter Selasky iface->parent_iface_index = 921d9073c1eSHans Petter Selasky USB_IFACE_INDEX_ANY; 922d9073c1eSHans Petter Selasky } 923bdd41206SAndrew Thompson } 924bdd41206SAndrew Thompson 925bdd41206SAndrew Thompson DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints); 926bdd41206SAndrew Thompson 927760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)id; 928bdd41206SAndrew Thompson 929bdd41206SAndrew Thompson temp = ep_curr; 930bdd41206SAndrew Thompson 931bdd41206SAndrew Thompson /* iterate all the endpoint descriptors */ 932a593f6b8SAndrew Thompson while ((ed = usb_edesc_foreach(udev->cdesc, ed))) { 9335b0752bbSHans Petter Selasky /* check if endpoint limit has been reached */ 9345b0752bbSHans Petter Selasky if (temp >= USB_MAX_EP_UNITS) { 9355b0752bbSHans Petter Selasky DPRINTF("Endpoint limit reached\n"); 9365b0752bbSHans Petter Selasky break; 9375b0752bbSHans Petter Selasky } 938bdd41206SAndrew Thompson 939ae60fdfbSAndrew Thompson ep = udev->endpoints + temp; 940bdd41206SAndrew Thompson 941bdd41206SAndrew Thompson if (do_init) { 942963169b4SHans Petter Selasky void *ecomp; 943963169b4SHans Petter Selasky 944963169b4SHans Petter Selasky ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed); 945963169b4SHans Petter Selasky if (ecomp != NULL) 946963169b4SHans Petter Selasky DPRINTFN(5, "Found endpoint companion descriptor\n"); 947963169b4SHans Petter Selasky 948a593f6b8SAndrew Thompson usb_init_endpoint(udev, 949963169b4SHans Petter Selasky ips.iface_index, ed, ecomp, ep); 950bdd41206SAndrew Thompson } 951bdd41206SAndrew Thompson 952bdd41206SAndrew Thompson temp ++; 953bdd41206SAndrew Thompson 954bdd41206SAndrew Thompson /* find maximum number of endpoints */ 955bdd41206SAndrew Thompson if (ep_max < temp) 956bdd41206SAndrew Thompson ep_max = temp; 957bdd41206SAndrew Thompson } 958bdd41206SAndrew Thompson } 959bdd41206SAndrew Thompson 960bdd41206SAndrew Thompson /* NOTE: It is valid to have no interfaces and no endpoints! */ 961bdd41206SAndrew Thompson 962bdd41206SAndrew Thompson if (cmd == USB_CFG_ALLOC) { 963bdd41206SAndrew Thompson udev->ifaces_max = ips.iface_index; 9645b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) 965bdd41206SAndrew Thompson udev->ifaces = NULL; 966bdd41206SAndrew Thompson if (udev->ifaces_max != 0) { 967bdd41206SAndrew Thompson udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max, 968bdd41206SAndrew Thompson M_USB, M_WAITOK | M_ZERO); 969bdd41206SAndrew Thompson if (udev->ifaces == NULL) { 970bdd41206SAndrew Thompson err = USB_ERR_NOMEM; 971bdd41206SAndrew Thompson goto done; 972bdd41206SAndrew Thompson } 973bdd41206SAndrew Thompson } 9745b0752bbSHans Petter Selasky #endif 9755b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0) 9769469c929SAndrew Thompson if (ep_max != 0) { 977ae60fdfbSAndrew Thompson udev->endpoints = malloc(sizeof(*ep) * ep_max, 978bdd41206SAndrew Thompson M_USB, M_WAITOK | M_ZERO); 979ae60fdfbSAndrew Thompson if (udev->endpoints == NULL) { 980bdd41206SAndrew Thompson err = USB_ERR_NOMEM; 981bdd41206SAndrew Thompson goto done; 982bdd41206SAndrew Thompson } 9839469c929SAndrew Thompson } else { 984ae60fdfbSAndrew Thompson udev->endpoints = NULL; 985bdd41206SAndrew Thompson } 9865b0752bbSHans Petter Selasky #endif 9879469c929SAndrew Thompson USB_BUS_LOCK(udev->bus); 988ae60fdfbSAndrew Thompson udev->endpoints_max = ep_max; 9899469c929SAndrew Thompson /* reset any ongoing clear-stall */ 990ae60fdfbSAndrew Thompson udev->ep_curr = NULL; 9919469c929SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 992bdd41206SAndrew Thompson } 9936c218933SHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0) 994bdd41206SAndrew Thompson done: 9956c218933SHans Petter Selasky #endif 996bdd41206SAndrew Thompson if (err) { 997bdd41206SAndrew Thompson if (cmd == USB_CFG_ALLOC) { 998bdd41206SAndrew Thompson cleanup: 9999469c929SAndrew Thompson USB_BUS_LOCK(udev->bus); 1000ae60fdfbSAndrew Thompson udev->endpoints_max = 0; 10019469c929SAndrew Thompson /* reset any ongoing clear-stall */ 1002ae60fdfbSAndrew Thompson udev->ep_curr = NULL; 10039469c929SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 10049469c929SAndrew Thompson 10055b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) 1006bdd41206SAndrew Thompson free(udev->ifaces, M_USB); 1007bdd41206SAndrew Thompson udev->ifaces = NULL; 10085b0752bbSHans Petter Selasky #endif 10095b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0) 10105b0752bbSHans Petter Selasky free(udev->endpoints, M_USB); 1011ae60fdfbSAndrew Thompson udev->endpoints = NULL; 10125b0752bbSHans Petter Selasky #endif 1013bdd41206SAndrew Thompson udev->ifaces_max = 0; 1014bdd41206SAndrew Thompson } 1015bdd41206SAndrew Thompson } 1016bdd41206SAndrew Thompson return (err); 1017bdd41206SAndrew Thompson } 1018bdd41206SAndrew Thompson 1019bdd41206SAndrew Thompson /*------------------------------------------------------------------------* 1020a593f6b8SAndrew Thompson * usbd_set_alt_interface_index 102102ac6454SAndrew Thompson * 102202ac6454SAndrew Thompson * This function will select an alternate interface index for the 102302ac6454SAndrew Thompson * given interface index. The interface should not be in use when this 1024e2805033SAndrew Thompson * function is called. That means there should not be any open USB 1025e2805033SAndrew Thompson * transfers. Else an error is returned. If the alternate setting is 1026e2805033SAndrew Thompson * already set this function will simply return success. This function 1027e2805033SAndrew Thompson * is called in Host mode and Device mode! 102802ac6454SAndrew Thompson * 102902ac6454SAndrew Thompson * Returns: 103002ac6454SAndrew Thompson * 0: Success 103102ac6454SAndrew Thompson * Else: Failure 103202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1033e0a69b51SAndrew Thompson usb_error_t 1034a593f6b8SAndrew Thompson usbd_set_alt_interface_index(struct usb_device *udev, 103502ac6454SAndrew Thompson uint8_t iface_index, uint8_t alt_index) 103602ac6454SAndrew Thompson { 1037a593f6b8SAndrew Thompson struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1038e0a69b51SAndrew Thompson usb_error_t err; 103902ac6454SAndrew Thompson uint8_t do_unlock; 104002ac6454SAndrew Thompson 10416950c75fSHans Petter Selasky /* Prevent re-enumeration */ 10426950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 10436950c75fSHans Petter Selasky 104402ac6454SAndrew Thompson if (iface == NULL) { 104502ac6454SAndrew Thompson err = USB_ERR_INVAL; 104602ac6454SAndrew Thompson goto done; 104702ac6454SAndrew Thompson } 1048e2805033SAndrew Thompson if (iface->alt_index == alt_index) { 1049e2805033SAndrew Thompson /* 1050e2805033SAndrew Thompson * Optimise away duplicate setting of 1051e2805033SAndrew Thompson * alternate setting in USB Host Mode! 1052e2805033SAndrew Thompson */ 1053e2805033SAndrew Thompson err = 0; 1054e2805033SAndrew Thompson goto done; 1055e2805033SAndrew Thompson } 10560f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 105702ac6454SAndrew Thompson /* 105802ac6454SAndrew Thompson * Free all generic FIFOs for this interface, except control 105902ac6454SAndrew Thompson * endpoint FIFOs: 106002ac6454SAndrew Thompson */ 1061a593f6b8SAndrew Thompson usb_fifo_free_wrap(udev, iface_index, 0); 10620f6e8f93SAndrew Thompson #endif 1063bdd41206SAndrew Thompson 1064a593f6b8SAndrew Thompson err = usb_config_parse(udev, iface_index, alt_index); 106502ac6454SAndrew Thompson if (err) { 106602ac6454SAndrew Thompson goto done; 106702ac6454SAndrew Thompson } 1068bd73b187SAlfred Perlstein if (iface->alt_index != alt_index) { 1069bd73b187SAlfred Perlstein /* the alternate setting does not exist */ 1070bd73b187SAlfred Perlstein err = USB_ERR_INVAL; 1071bd73b187SAlfred Perlstein goto done; 1072bd73b187SAlfred Perlstein } 1073bd73b187SAlfred Perlstein 1074a593f6b8SAndrew Thompson err = usbd_req_set_alt_interface_no(udev, NULL, iface_index, 107502ac6454SAndrew Thompson iface->idesc->bAlternateSetting); 107602ac6454SAndrew Thompson 107702ac6454SAndrew Thompson done: 1078cb18f7d1SAlfred Perlstein if (do_unlock) 1079cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 108002ac6454SAndrew Thompson return (err); 108102ac6454SAndrew Thompson } 108202ac6454SAndrew Thompson 108302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1084a593f6b8SAndrew Thompson * usbd_set_endpoint_stall 108502ac6454SAndrew Thompson * 1086963169b4SHans Petter Selasky * This function is used to make a BULK or INTERRUPT endpoint send 1087963169b4SHans Petter Selasky * STALL tokens in USB device mode. 108802ac6454SAndrew Thompson * 108902ac6454SAndrew Thompson * Returns: 109002ac6454SAndrew Thompson * 0: Success 109102ac6454SAndrew Thompson * Else: Failure 109202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1093e0a69b51SAndrew Thompson usb_error_t 1094a593f6b8SAndrew Thompson usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep, 109502ac6454SAndrew Thompson uint8_t do_stall) 109602ac6454SAndrew Thompson { 1097760bc48eSAndrew Thompson struct usb_xfer *xfer; 1098a5cf1aaaSHans Petter Selasky usb_stream_t x; 109902ac6454SAndrew Thompson uint8_t et; 110002ac6454SAndrew Thompson uint8_t was_stalled; 110102ac6454SAndrew Thompson 1102ae60fdfbSAndrew Thompson if (ep == NULL) { 110302ac6454SAndrew Thompson /* nothing to do */ 110402ac6454SAndrew Thompson DPRINTF("Cannot find endpoint\n"); 110502ac6454SAndrew Thompson /* 110602ac6454SAndrew Thompson * Pretend that the clear or set stall request is 110702ac6454SAndrew Thompson * successful else some USB host stacks can do 110802ac6454SAndrew Thompson * strange things, especially when a control endpoint 110902ac6454SAndrew Thompson * stalls. 111002ac6454SAndrew Thompson */ 111102ac6454SAndrew Thompson return (0); 111202ac6454SAndrew Thompson } 1113ae60fdfbSAndrew Thompson et = (ep->edesc->bmAttributes & UE_XFERTYPE); 111402ac6454SAndrew Thompson 111502ac6454SAndrew Thompson if ((et != UE_BULK) && 111602ac6454SAndrew Thompson (et != UE_INTERRUPT)) { 111702ac6454SAndrew Thompson /* 111802ac6454SAndrew Thompson * Should not stall control 111902ac6454SAndrew Thompson * nor isochronous endpoints. 112002ac6454SAndrew Thompson */ 112102ac6454SAndrew Thompson DPRINTF("Invalid endpoint\n"); 112202ac6454SAndrew Thompson return (0); 112302ac6454SAndrew Thompson } 112402ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 112502ac6454SAndrew Thompson 112602ac6454SAndrew Thompson /* store current stall state */ 1127ae60fdfbSAndrew Thompson was_stalled = ep->is_stalled; 112802ac6454SAndrew Thompson 112902ac6454SAndrew Thompson /* check for no change */ 113002ac6454SAndrew Thompson if (was_stalled && do_stall) { 1131ae60fdfbSAndrew Thompson /* if the endpoint is already stalled do nothing */ 113202ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 113302ac6454SAndrew Thompson DPRINTF("No change\n"); 113402ac6454SAndrew Thompson return (0); 113502ac6454SAndrew Thompson } 113602ac6454SAndrew Thompson /* set stalled state */ 1137ae60fdfbSAndrew Thompson ep->is_stalled = 1; 113802ac6454SAndrew Thompson 113902ac6454SAndrew Thompson if (do_stall || (!was_stalled)) { 114002ac6454SAndrew Thompson if (!was_stalled) { 1141a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 114202ac6454SAndrew Thompson /* lookup the current USB transfer, if any */ 1143a5cf1aaaSHans Petter Selasky xfer = ep->endpoint_q[x].curr; 1144a5cf1aaaSHans Petter Selasky if (xfer != NULL) { 114502ac6454SAndrew Thompson /* 1146a5cf1aaaSHans Petter Selasky * The "xfer_stall" method 1147a5cf1aaaSHans Petter Selasky * will complete the USB 1148a5cf1aaaSHans Petter Selasky * transfer like in case of a 1149a5cf1aaaSHans Petter Selasky * timeout setting the error 1150a5cf1aaaSHans Petter Selasky * code "USB_ERR_STALLED". 115102ac6454SAndrew Thompson */ 1152a5cf1aaaSHans Petter Selasky (udev->bus->methods->xfer_stall) (xfer); 1153a5cf1aaaSHans Petter Selasky } 1154a5cf1aaaSHans Petter Selasky } 1155a5cf1aaaSHans Petter Selasky } 1156a5cf1aaaSHans Petter Selasky (udev->bus->methods->set_stall) (udev, ep, &do_stall); 115702ac6454SAndrew Thompson } 115802ac6454SAndrew Thompson if (!do_stall) { 1159ae60fdfbSAndrew Thompson ep->toggle_next = 0; /* reset data toggle */ 1160ae60fdfbSAndrew Thompson ep->is_stalled = 0; /* clear stalled state */ 116102ac6454SAndrew Thompson 1162ae60fdfbSAndrew Thompson (udev->bus->methods->clear_stall) (udev, ep); 116302ac6454SAndrew Thompson 1164a5cf1aaaSHans Petter Selasky /* start the current or next transfer, if any */ 1165a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 1166a5cf1aaaSHans Petter Selasky usb_command_wrapper(&ep->endpoint_q[x], 1167a5cf1aaaSHans Petter Selasky ep->endpoint_q[x].curr); 1168a5cf1aaaSHans Petter Selasky } 116902ac6454SAndrew Thompson } 117002ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 117102ac6454SAndrew Thompson return (0); 117202ac6454SAndrew Thompson } 117302ac6454SAndrew Thompson 117402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1175a593f6b8SAndrew Thompson * usb_reset_iface_endpoints - used in USB device side mode 117602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1177e0a69b51SAndrew Thompson usb_error_t 1178a593f6b8SAndrew Thompson usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index) 117902ac6454SAndrew Thompson { 1180ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 1181ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end; 118202ac6454SAndrew Thompson 1183ae60fdfbSAndrew Thompson ep = udev->endpoints; 1184ae60fdfbSAndrew Thompson ep_end = udev->endpoints + udev->endpoints_max; 118502ac6454SAndrew Thompson 1186ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 1187ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 1188ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 118902ac6454SAndrew Thompson continue; 119002ac6454SAndrew Thompson } 119102ac6454SAndrew Thompson /* simulate a clear stall from the peer */ 1192bd73b187SAlfred Perlstein usbd_set_endpoint_stall(udev, ep, 0); 119302ac6454SAndrew Thompson } 119402ac6454SAndrew Thompson return (0); 119502ac6454SAndrew Thompson } 119602ac6454SAndrew Thompson 119702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1198a593f6b8SAndrew Thompson * usb_detach_device_sub 119902ac6454SAndrew Thompson * 120002ac6454SAndrew Thompson * This function will try to detach an USB device. If it fails a panic 120102ac6454SAndrew Thompson * will result. 1202bdd41206SAndrew Thompson * 1203bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 120402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 120502ac6454SAndrew Thompson static void 1206a593f6b8SAndrew Thompson usb_detach_device_sub(struct usb_device *udev, device_t *ppdev, 12078427ed84SHans Petter Selasky char **ppnpinfo, uint8_t flag) 120802ac6454SAndrew Thompson { 120902ac6454SAndrew Thompson device_t dev; 12108427ed84SHans Petter Selasky char *pnpinfo; 121102ac6454SAndrew Thompson int err; 121202ac6454SAndrew Thompson 1213d88688c7SAndrew Thompson dev = *ppdev; 1214d88688c7SAndrew Thompson if (dev) { 121502ac6454SAndrew Thompson /* 121602ac6454SAndrew Thompson * NOTE: It is important to clear "*ppdev" before deleting 121702ac6454SAndrew Thompson * the child due to some device methods being called late 121802ac6454SAndrew Thompson * during the delete process ! 121902ac6454SAndrew Thompson */ 122002ac6454SAndrew Thompson *ppdev = NULL; 122102ac6454SAndrew Thompson 1222cabe79d9SMarius Strobl if (!rebooting) { 122302ac6454SAndrew Thompson device_printf(dev, "at %s, port %d, addr %d " 122402ac6454SAndrew Thompson "(disconnected)\n", 122502ac6454SAndrew Thompson device_get_nameunit(udev->parent_dev), 122602ac6454SAndrew Thompson udev->port_no, udev->address); 1227cabe79d9SMarius Strobl } 122802ac6454SAndrew Thompson 122902ac6454SAndrew Thompson if (device_is_attached(dev)) { 1230ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 123102ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 123202ac6454SAndrew Thompson if (err) { 1233767cb2e2SAndrew Thompson device_printf(dev, "Resume failed\n"); 123402ac6454SAndrew Thompson } 123502ac6454SAndrew Thompson } 123602ac6454SAndrew Thompson } 1237d3bf5efcSHans Petter Selasky /* detach and delete child */ 123802ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 123902ac6454SAndrew Thompson goto error; 124002ac6454SAndrew Thompson } 124102ac6454SAndrew Thompson } 12428427ed84SHans Petter Selasky 12438427ed84SHans Petter Selasky pnpinfo = *ppnpinfo; 12448427ed84SHans Petter Selasky if (pnpinfo != NULL) { 12458427ed84SHans Petter Selasky *ppnpinfo = NULL; 12468427ed84SHans Petter Selasky free(pnpinfo, M_USBDEV); 12478427ed84SHans Petter Selasky } 124802ac6454SAndrew Thompson return; 124902ac6454SAndrew Thompson 125002ac6454SAndrew Thompson error: 125102ac6454SAndrew Thompson /* Detach is not allowed to fail in the USB world */ 12528427ed84SHans Petter Selasky panic("usb_detach_device_sub: A USB driver would not detach\n"); 125302ac6454SAndrew Thompson } 125402ac6454SAndrew Thompson 125502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1256a593f6b8SAndrew Thompson * usb_detach_device 125702ac6454SAndrew Thompson * 125802ac6454SAndrew Thompson * The following function will detach the matching interfaces. 125902ac6454SAndrew Thompson * This function is NULL safe. 1260bdd41206SAndrew Thompson * 1261bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 126202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 126302ac6454SAndrew Thompson void 1264a593f6b8SAndrew Thompson usb_detach_device(struct usb_device *udev, uint8_t iface_index, 1265bdd41206SAndrew Thompson uint8_t flag) 126602ac6454SAndrew Thompson { 1267760bc48eSAndrew Thompson struct usb_interface *iface; 126802ac6454SAndrew Thompson uint8_t i; 126902ac6454SAndrew Thompson 127002ac6454SAndrew Thompson if (udev == NULL) { 127102ac6454SAndrew Thompson /* nothing to do */ 127202ac6454SAndrew Thompson return; 127302ac6454SAndrew Thompson } 127402ac6454SAndrew Thompson DPRINTFN(4, "udev=%p\n", udev); 127502ac6454SAndrew Thompson 127691cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 127702ac6454SAndrew Thompson 127802ac6454SAndrew Thompson /* 127902ac6454SAndrew Thompson * First detach the child to give the child's detach routine a 128002ac6454SAndrew Thompson * chance to detach the sub-devices in the correct order. 128102ac6454SAndrew Thompson * Then delete the child using "device_delete_child()" which 128202ac6454SAndrew Thompson * will detach all sub-devices from the bottom and upwards! 128302ac6454SAndrew Thompson */ 128402ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 128502ac6454SAndrew Thompson i = iface_index; 128602ac6454SAndrew Thompson iface_index = i + 1; 128702ac6454SAndrew Thompson } else { 128802ac6454SAndrew Thompson i = 0; 128902ac6454SAndrew Thompson iface_index = USB_IFACE_MAX; 129002ac6454SAndrew Thompson } 129102ac6454SAndrew Thompson 129202ac6454SAndrew Thompson /* do the detach */ 129302ac6454SAndrew Thompson 129402ac6454SAndrew Thompson for (; i != iface_index; i++) { 1295a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 129602ac6454SAndrew Thompson if (iface == NULL) { 129702ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 129802ac6454SAndrew Thompson break; 129902ac6454SAndrew Thompson } 13008427ed84SHans Petter Selasky usb_detach_device_sub(udev, &iface->subdev, 13018427ed84SHans Petter Selasky &iface->pnpinfo, flag); 130202ac6454SAndrew Thompson } 130302ac6454SAndrew Thompson } 130402ac6454SAndrew Thompson 130502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1306a593f6b8SAndrew Thompson * usb_probe_and_attach_sub 130702ac6454SAndrew Thompson * 130802ac6454SAndrew Thompson * Returns: 130902ac6454SAndrew Thompson * 0: Success 131002ac6454SAndrew Thompson * Else: Failure 131102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 131202ac6454SAndrew Thompson static uint8_t 1313a593f6b8SAndrew Thompson usb_probe_and_attach_sub(struct usb_device *udev, 1314760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 131502ac6454SAndrew Thompson { 1316760bc48eSAndrew Thompson struct usb_interface *iface; 131702ac6454SAndrew Thompson device_t dev; 131802ac6454SAndrew Thompson int err; 131902ac6454SAndrew Thompson 132002ac6454SAndrew Thompson iface = uaa->iface; 132102ac6454SAndrew Thompson if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 132202ac6454SAndrew Thompson /* leave interface alone */ 132302ac6454SAndrew Thompson return (0); 132402ac6454SAndrew Thompson } 132502ac6454SAndrew Thompson dev = iface->subdev; 132602ac6454SAndrew Thompson if (dev) { 132702ac6454SAndrew Thompson /* clean up after module unload */ 132802ac6454SAndrew Thompson 132902ac6454SAndrew Thompson if (device_is_attached(dev)) { 133002ac6454SAndrew Thompson /* already a device there */ 133102ac6454SAndrew Thompson return (0); 133202ac6454SAndrew Thompson } 133302ac6454SAndrew Thompson /* clear "iface->subdev" as early as possible */ 133402ac6454SAndrew Thompson 133502ac6454SAndrew Thompson iface->subdev = NULL; 133602ac6454SAndrew Thompson 133702ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 133802ac6454SAndrew Thompson /* 133902ac6454SAndrew Thompson * Panic here, else one can get a double call 134002ac6454SAndrew Thompson * to device_detach(). USB devices should 134102ac6454SAndrew Thompson * never fail on detach! 134202ac6454SAndrew Thompson */ 1343767cb2e2SAndrew Thompson panic("device_delete_child() failed\n"); 134402ac6454SAndrew Thompson } 134502ac6454SAndrew Thompson } 134602ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 134702ac6454SAndrew Thompson /* create a new child */ 134802ac6454SAndrew Thompson uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1); 134902ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 135002ac6454SAndrew Thompson device_printf(udev->parent_dev, 1351767cb2e2SAndrew Thompson "Device creation failed\n"); 135202ac6454SAndrew Thompson return (1); /* failure */ 135302ac6454SAndrew Thompson } 135402ac6454SAndrew Thompson device_set_ivars(uaa->temp_dev, uaa); 135502ac6454SAndrew Thompson device_quiet(uaa->temp_dev); 135602ac6454SAndrew Thompson } 135702ac6454SAndrew Thompson /* 135802ac6454SAndrew Thompson * Set "subdev" before probe and attach so that "devd" gets 135902ac6454SAndrew Thompson * the information it needs. 136002ac6454SAndrew Thompson */ 136102ac6454SAndrew Thompson iface->subdev = uaa->temp_dev; 136202ac6454SAndrew Thompson 136302ac6454SAndrew Thompson if (device_probe_and_attach(iface->subdev) == 0) { 136402ac6454SAndrew Thompson /* 136502ac6454SAndrew Thompson * The USB attach arguments are only available during probe 136602ac6454SAndrew Thompson * and attach ! 136702ac6454SAndrew Thompson */ 136802ac6454SAndrew Thompson uaa->temp_dev = NULL; 136902ac6454SAndrew Thompson device_set_ivars(iface->subdev, NULL); 137002ac6454SAndrew Thompson 1371ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 137202ac6454SAndrew Thompson err = DEVICE_SUSPEND(iface->subdev); 137388b4e0abSAndrew Thompson if (err) 137402ac6454SAndrew Thompson device_printf(iface->subdev, "Suspend failed\n"); 137502ac6454SAndrew Thompson } 137602ac6454SAndrew Thompson return (0); /* success */ 137702ac6454SAndrew Thompson } else { 137802ac6454SAndrew Thompson /* No USB driver found */ 137902ac6454SAndrew Thompson iface->subdev = NULL; 138002ac6454SAndrew Thompson } 138102ac6454SAndrew Thompson return (1); /* failure */ 138202ac6454SAndrew Thompson } 138302ac6454SAndrew Thompson 138402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1385a593f6b8SAndrew Thompson * usbd_set_parent_iface 138602ac6454SAndrew Thompson * 138702ac6454SAndrew Thompson * Using this function will lock the alternate interface setting on an 138802ac6454SAndrew Thompson * interface. It is typically used for multi interface drivers. In USB 138902ac6454SAndrew Thompson * device side mode it is assumed that the alternate interfaces all 139002ac6454SAndrew Thompson * have the same endpoint descriptors. The default parent index value 139102ac6454SAndrew Thompson * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 139202ac6454SAndrew Thompson * locked. 139302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 139402ac6454SAndrew Thompson void 1395a593f6b8SAndrew Thompson usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index, 139602ac6454SAndrew Thompson uint8_t parent_index) 139702ac6454SAndrew Thompson { 1398760bc48eSAndrew Thompson struct usb_interface *iface; 139902ac6454SAndrew Thompson 14005e055201SHans Petter Selasky if (udev == NULL || iface_index == parent_index) { 1401d9073c1eSHans Petter Selasky /* nothing to do */ 1402d9073c1eSHans Petter Selasky return; 140302ac6454SAndrew Thompson } 1404d9073c1eSHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 1405d9073c1eSHans Petter Selasky if (iface != NULL) 1406d9073c1eSHans Petter Selasky iface->parent_iface_index = parent_index; 140702ac6454SAndrew Thompson } 140802ac6454SAndrew Thompson 140902ac6454SAndrew Thompson static void 1410a593f6b8SAndrew Thompson usb_init_attach_arg(struct usb_device *udev, 1411760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 141202ac6454SAndrew Thompson { 1413d46dc4adSHans Petter Selasky memset(uaa, 0, sizeof(*uaa)); 141402ac6454SAndrew Thompson 141502ac6454SAndrew Thompson uaa->device = udev; 1416f29a0724SAndrew Thompson uaa->usb_mode = udev->flags.usb_mode; 141702ac6454SAndrew Thompson uaa->port = udev->port_no; 14182a4c6157SAndrew Thompson uaa->dev_state = UAA_DEV_READY; 141902ac6454SAndrew Thompson 142002ac6454SAndrew Thompson uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 142102ac6454SAndrew Thompson uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 142202ac6454SAndrew Thompson uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 142302ac6454SAndrew Thompson uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 142402ac6454SAndrew Thompson uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 142502ac6454SAndrew Thompson uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 142602ac6454SAndrew Thompson uaa->info.bConfigIndex = udev->curr_config_index; 142702ac6454SAndrew Thompson uaa->info.bConfigNum = udev->curr_config_no; 142802ac6454SAndrew Thompson } 142902ac6454SAndrew Thompson 143002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1431a593f6b8SAndrew Thompson * usb_probe_and_attach 143202ac6454SAndrew Thompson * 143302ac6454SAndrew Thompson * This function is called from "uhub_explore_sub()", 1434a593f6b8SAndrew Thompson * "usb_handle_set_config()" and "usb_handle_request()". 143502ac6454SAndrew Thompson * 143602ac6454SAndrew Thompson * Returns: 143702ac6454SAndrew Thompson * 0: Success 143802ac6454SAndrew Thompson * Else: A control transfer failed 143902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1440e0a69b51SAndrew Thompson usb_error_t 1441a593f6b8SAndrew Thompson usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index) 144202ac6454SAndrew Thompson { 1443760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1444760bc48eSAndrew Thompson struct usb_interface *iface; 144502ac6454SAndrew Thompson uint8_t i; 144602ac6454SAndrew Thompson uint8_t j; 144702ac6454SAndrew Thompson uint8_t do_unlock; 144802ac6454SAndrew Thompson 144902ac6454SAndrew Thompson if (udev == NULL) { 145002ac6454SAndrew Thompson DPRINTF("udev == NULL\n"); 145102ac6454SAndrew Thompson return (USB_ERR_INVAL); 145202ac6454SAndrew Thompson } 14536950c75fSHans Petter Selasky /* Prevent re-enumeration */ 14546950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 145502ac6454SAndrew Thompson 145602ac6454SAndrew Thompson if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 145702ac6454SAndrew Thompson /* do nothing - no configuration has been set */ 145802ac6454SAndrew Thompson goto done; 145902ac6454SAndrew Thompson } 146002ac6454SAndrew Thompson /* setup USB attach arguments */ 146102ac6454SAndrew Thompson 1462a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 146302ac6454SAndrew Thompson 1464a6b60150SHans Petter Selasky /* 1465a6b60150SHans Petter Selasky * If the whole USB device is targeted, invoke the USB event 1466a6b60150SHans Petter Selasky * handler(s): 1467a6b60150SHans Petter Selasky */ 1468a6b60150SHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 1469415bcd89SHans Petter Selasky if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 && 1470415bcd89SHans Petter Selasky usb_dymo_eject(udev, 0) == 0) { 1471415bcd89SHans Petter Selasky /* success, mark the udev as disappearing */ 1472415bcd89SHans Petter Selasky uaa.dev_state = UAA_DEV_EJECTING; 1473415bcd89SHans Petter Selasky } 1474415bcd89SHans Petter Selasky 1475a6b60150SHans Petter Selasky EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa); 1476a6b60150SHans Petter Selasky 1477a6b60150SHans Petter Selasky if (uaa.dev_state != UAA_DEV_READY) { 1478a6b60150SHans Petter Selasky /* leave device unconfigured */ 1479a6b60150SHans Petter Selasky usb_unconfigure(udev, 0); 1480a6b60150SHans Petter Selasky goto done; 1481a6b60150SHans Petter Selasky } 1482a6b60150SHans Petter Selasky } 1483a6b60150SHans Petter Selasky 148402ac6454SAndrew Thompson /* Check if only one interface should be probed: */ 148502ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 148602ac6454SAndrew Thompson i = iface_index; 148702ac6454SAndrew Thompson j = i + 1; 148802ac6454SAndrew Thompson } else { 148902ac6454SAndrew Thompson i = 0; 149002ac6454SAndrew Thompson j = USB_IFACE_MAX; 149102ac6454SAndrew Thompson } 149202ac6454SAndrew Thompson 149302ac6454SAndrew Thompson /* Do the probe and attach */ 149402ac6454SAndrew Thompson for (; i != j; i++) { 1495a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 149602ac6454SAndrew Thompson if (iface == NULL) { 149702ac6454SAndrew Thompson /* 149802ac6454SAndrew Thompson * Looks like the end of the USB 149902ac6454SAndrew Thompson * interfaces ! 150002ac6454SAndrew Thompson */ 150102ac6454SAndrew Thompson DPRINTFN(2, "end of interfaces " 150202ac6454SAndrew Thompson "at %u\n", i); 150302ac6454SAndrew Thompson break; 150402ac6454SAndrew Thompson } 150502ac6454SAndrew Thompson if (iface->idesc == NULL) { 150602ac6454SAndrew Thompson /* no interface descriptor */ 150702ac6454SAndrew Thompson continue; 150802ac6454SAndrew Thompson } 150902ac6454SAndrew Thompson uaa.iface = iface; 151002ac6454SAndrew Thompson 151102ac6454SAndrew Thompson uaa.info.bInterfaceClass = 151202ac6454SAndrew Thompson iface->idesc->bInterfaceClass; 151302ac6454SAndrew Thompson uaa.info.bInterfaceSubClass = 151402ac6454SAndrew Thompson iface->idesc->bInterfaceSubClass; 151502ac6454SAndrew Thompson uaa.info.bInterfaceProtocol = 151602ac6454SAndrew Thompson iface->idesc->bInterfaceProtocol; 151702ac6454SAndrew Thompson uaa.info.bIfaceIndex = i; 151802ac6454SAndrew Thompson uaa.info.bIfaceNum = 151902ac6454SAndrew Thompson iface->idesc->bInterfaceNumber; 1520bd73b187SAlfred Perlstein uaa.driver_info = 0; /* reset driver_info */ 152102ac6454SAndrew Thompson 152202ac6454SAndrew Thompson DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 152302ac6454SAndrew Thompson uaa.info.bInterfaceClass, 152402ac6454SAndrew Thompson uaa.info.bInterfaceSubClass, 152502ac6454SAndrew Thompson uaa.info.bInterfaceProtocol, 152602ac6454SAndrew Thompson uaa.info.bIfaceIndex, 152702ac6454SAndrew Thompson uaa.info.bIfaceNum); 152802ac6454SAndrew Thompson 1529d43ffe94SHans Petter Selasky usb_probe_and_attach_sub(udev, &uaa); 153002ac6454SAndrew Thompson 1531d43ffe94SHans Petter Selasky /* 1532d43ffe94SHans Petter Selasky * Remove the leftover child, if any, to enforce that 1533d43ffe94SHans Petter Selasky * a new nomatch devd event is generated for the next 1534d43ffe94SHans Petter Selasky * interface if no driver is found: 1535d43ffe94SHans Petter Selasky */ 1536d43ffe94SHans Petter Selasky if (uaa.temp_dev == NULL) 1537d43ffe94SHans Petter Selasky continue; 1538d43ffe94SHans Petter Selasky if (device_delete_child(udev->parent_dev, uaa.temp_dev)) 1539767cb2e2SAndrew Thompson DPRINTFN(0, "device delete child failed\n"); 1540d43ffe94SHans Petter Selasky uaa.temp_dev = NULL; 154102ac6454SAndrew Thompson } 154202ac6454SAndrew Thompson done: 1543cb18f7d1SAlfred Perlstein if (do_unlock) 1544cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 154502ac6454SAndrew Thompson return (0); 154602ac6454SAndrew Thompson } 154702ac6454SAndrew Thompson 154802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1549a593f6b8SAndrew Thompson * usb_suspend_resume_sub 155002ac6454SAndrew Thompson * 155102ac6454SAndrew Thompson * This function is called when the suspend or resume methods should 155202ac6454SAndrew Thompson * be executed on an USB device. 155302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 155402ac6454SAndrew Thompson static void 1555a593f6b8SAndrew Thompson usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend) 155602ac6454SAndrew Thompson { 155702ac6454SAndrew Thompson int err; 155802ac6454SAndrew Thompson 155902ac6454SAndrew Thompson if (dev == NULL) { 156002ac6454SAndrew Thompson return; 156102ac6454SAndrew Thompson } 156202ac6454SAndrew Thompson if (!device_is_attached(dev)) { 156302ac6454SAndrew Thompson return; 156402ac6454SAndrew Thompson } 156502ac6454SAndrew Thompson if (do_suspend) { 156602ac6454SAndrew Thompson err = DEVICE_SUSPEND(dev); 156702ac6454SAndrew Thompson } else { 156802ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 156902ac6454SAndrew Thompson } 157002ac6454SAndrew Thompson if (err) { 1571767cb2e2SAndrew Thompson device_printf(dev, "%s failed\n", 157202ac6454SAndrew Thompson do_suspend ? "Suspend" : "Resume"); 157302ac6454SAndrew Thompson } 157402ac6454SAndrew Thompson } 157502ac6454SAndrew Thompson 157602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1577a593f6b8SAndrew Thompson * usb_suspend_resume 157802ac6454SAndrew Thompson * 157902ac6454SAndrew Thompson * The following function will suspend or resume the USB device. 158002ac6454SAndrew Thompson * 158102ac6454SAndrew Thompson * Returns: 158202ac6454SAndrew Thompson * 0: Success 158302ac6454SAndrew Thompson * Else: Failure 158402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1585e0a69b51SAndrew Thompson usb_error_t 1586a593f6b8SAndrew Thompson usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend) 158702ac6454SAndrew Thompson { 1588760bc48eSAndrew Thompson struct usb_interface *iface; 158902ac6454SAndrew Thompson uint8_t i; 159002ac6454SAndrew Thompson 159102ac6454SAndrew Thompson if (udev == NULL) { 159202ac6454SAndrew Thompson /* nothing to do */ 159302ac6454SAndrew Thompson return (0); 159402ac6454SAndrew Thompson } 159502ac6454SAndrew Thompson DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 159602ac6454SAndrew Thompson 15972df1e9a6SAndrew Thompson sx_assert(&udev->sr_sx, SA_LOCKED); 159802ac6454SAndrew Thompson 159902ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 160002ac6454SAndrew Thompson /* filter the suspend events */ 1601ec8f3127SAndrew Thompson if (udev->flags.peer_suspended == do_suspend) { 160202ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 160302ac6454SAndrew Thompson /* nothing to do */ 160402ac6454SAndrew Thompson return (0); 160502ac6454SAndrew Thompson } 1606ec8f3127SAndrew Thompson udev->flags.peer_suspended = do_suspend; 160702ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 160802ac6454SAndrew Thompson 160902ac6454SAndrew Thompson /* do the suspend or resume */ 161002ac6454SAndrew Thompson 161102ac6454SAndrew Thompson for (i = 0; i != USB_IFACE_MAX; i++) { 1612a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 161302ac6454SAndrew Thompson if (iface == NULL) { 161402ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 161502ac6454SAndrew Thompson break; 161602ac6454SAndrew Thompson } 1617a593f6b8SAndrew Thompson usb_suspend_resume_sub(udev, iface->subdev, do_suspend); 161802ac6454SAndrew Thompson } 161902ac6454SAndrew Thompson return (0); 162002ac6454SAndrew Thompson } 162102ac6454SAndrew Thompson 162202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1623a593f6b8SAndrew Thompson * usbd_clear_stall_proc 162402ac6454SAndrew Thompson * 162502ac6454SAndrew Thompson * This function performs generic USB clear stall operations. 162602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 162702ac6454SAndrew Thompson static void 1628a593f6b8SAndrew Thompson usbd_clear_stall_proc(struct usb_proc_msg *_pm) 162902ac6454SAndrew Thompson { 1630a0d53e0bSHans Petter Selasky struct usb_udev_msg *pm = (void *)_pm; 1631760bc48eSAndrew Thompson struct usb_device *udev = pm->udev; 163202ac6454SAndrew Thompson 163302ac6454SAndrew Thompson /* Change lock */ 163402ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 16350eb8d462SHans Petter Selasky USB_MTX_LOCK(&udev->device_mtx); 163602ac6454SAndrew Thompson 163702ac6454SAndrew Thompson /* Start clear stall callback */ 16385b3bb704SAndrew Thompson usbd_transfer_start(udev->ctrl_xfer[1]); 163902ac6454SAndrew Thompson 164002ac6454SAndrew Thompson /* Change lock */ 16410eb8d462SHans Petter Selasky USB_MTX_UNLOCK(&udev->device_mtx); 164202ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 164302ac6454SAndrew Thompson } 164402ac6454SAndrew Thompson 164502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1646f54ab96dSHans Petter Selasky * usb_get_langid 1647f54ab96dSHans Petter Selasky * 1648f54ab96dSHans Petter Selasky * This function tries to figure out the USB string language to use. 1649f54ab96dSHans Petter Selasky *------------------------------------------------------------------------*/ 1650f54ab96dSHans Petter Selasky void 1651f54ab96dSHans Petter Selasky usb_get_langid(struct usb_device *udev) 1652f54ab96dSHans Petter Selasky { 1653f54ab96dSHans Petter Selasky uint8_t *scratch_ptr; 1654f54ab96dSHans Petter Selasky uint8_t do_unlock; 1655f54ab96dSHans Petter Selasky int err; 1656f54ab96dSHans Petter Selasky 1657f54ab96dSHans Petter Selasky /* 1658f54ab96dSHans Petter Selasky * Workaround for buggy USB devices. 1659f54ab96dSHans Petter Selasky * 1660f54ab96dSHans Petter Selasky * It appears that some string-less USB chips will crash and 1661f54ab96dSHans Petter Selasky * disappear if any attempts are made to read any string 1662f54ab96dSHans Petter Selasky * descriptors. 1663f54ab96dSHans Petter Selasky * 1664f54ab96dSHans Petter Selasky * Try to detect such chips by checking the strings in the USB 1665f54ab96dSHans Petter Selasky * device descriptor. If no strings are present there we 1666f54ab96dSHans Petter Selasky * simply disable all USB strings. 1667f54ab96dSHans Petter Selasky */ 1668f54ab96dSHans Petter Selasky 1669f54ab96dSHans Petter Selasky /* Protect scratch area */ 1670f54ab96dSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 1671f54ab96dSHans Petter Selasky 1672f54ab96dSHans Petter Selasky scratch_ptr = udev->scratch.data; 1673f54ab96dSHans Petter Selasky 1674f54ab96dSHans Petter Selasky if (udev->flags.no_strings) { 1675f54ab96dSHans Petter Selasky err = USB_ERR_INVAL; 1676f54ab96dSHans Petter Selasky } else if (udev->ddesc.iManufacturer || 1677f54ab96dSHans Petter Selasky udev->ddesc.iProduct || 1678f54ab96dSHans Petter Selasky udev->ddesc.iSerialNumber) { 1679f54ab96dSHans Petter Selasky /* read out the language ID string */ 1680f54ab96dSHans Petter Selasky err = usbd_req_get_string_desc(udev, NULL, 1681f54ab96dSHans Petter Selasky (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE); 1682f54ab96dSHans Petter Selasky } else { 1683f54ab96dSHans Petter Selasky err = USB_ERR_INVAL; 1684f54ab96dSHans Petter Selasky } 1685f54ab96dSHans Petter Selasky 1686f54ab96dSHans Petter Selasky if (err || (scratch_ptr[0] < 4)) { 1687f54ab96dSHans Petter Selasky udev->flags.no_strings = 1; 1688f54ab96dSHans Petter Selasky } else { 1689f54ab96dSHans Petter Selasky uint16_t langid; 1690f54ab96dSHans Petter Selasky uint16_t pref; 1691f54ab96dSHans Petter Selasky uint16_t mask; 1692f54ab96dSHans Petter Selasky uint8_t x; 1693f54ab96dSHans Petter Selasky 1694f54ab96dSHans Petter Selasky /* load preferred value and mask */ 1695f54ab96dSHans Petter Selasky pref = usb_lang_id; 1696f54ab96dSHans Petter Selasky mask = usb_lang_mask; 1697f54ab96dSHans Petter Selasky 1698f54ab96dSHans Petter Selasky /* align length correctly */ 1699f54ab96dSHans Petter Selasky scratch_ptr[0] &= ~1U; 1700f54ab96dSHans Petter Selasky 1701f54ab96dSHans Petter Selasky /* fix compiler warning */ 1702f54ab96dSHans Petter Selasky langid = 0; 1703f54ab96dSHans Petter Selasky 1704f54ab96dSHans Petter Selasky /* search for preferred language */ 1705f54ab96dSHans Petter Selasky for (x = 2; x < scratch_ptr[0]; x += 2) { 1706f54ab96dSHans Petter Selasky langid = UGETW(scratch_ptr + x); 1707f54ab96dSHans Petter Selasky if ((langid & mask) == pref) 1708f54ab96dSHans Petter Selasky break; 1709f54ab96dSHans Petter Selasky } 1710f54ab96dSHans Petter Selasky if (x >= scratch_ptr[0]) { 1711f54ab96dSHans Petter Selasky /* pick the first language as the default */ 1712f54ab96dSHans Petter Selasky DPRINTFN(1, "Using first language\n"); 1713f54ab96dSHans Petter Selasky langid = UGETW(scratch_ptr + 2); 1714f54ab96dSHans Petter Selasky } 1715f54ab96dSHans Petter Selasky 1716f54ab96dSHans Petter Selasky DPRINTFN(1, "Language selected: 0x%04x\n", langid); 1717f54ab96dSHans Petter Selasky udev->langid = langid; 1718f54ab96dSHans Petter Selasky } 1719f54ab96dSHans Petter Selasky 1720f54ab96dSHans Petter Selasky if (do_unlock) 1721f54ab96dSHans Petter Selasky usbd_ctrl_unlock(udev); 1722f54ab96dSHans Petter Selasky } 1723f54ab96dSHans Petter Selasky 1724f54ab96dSHans Petter Selasky /*------------------------------------------------------------------------* 1725a593f6b8SAndrew Thompson * usb_alloc_device 172602ac6454SAndrew Thompson * 172702ac6454SAndrew Thompson * This function allocates a new USB device. This function is called 172802ac6454SAndrew Thompson * when a new device has been put in the powered state, but not yet in 172902ac6454SAndrew Thompson * the addressed state. Get initial descriptor, set the address, get 173002ac6454SAndrew Thompson * full descriptor and get strings. 173102ac6454SAndrew Thompson * 173202ac6454SAndrew Thompson * Return values: 173302ac6454SAndrew Thompson * 0: Failure 173402ac6454SAndrew Thompson * Else: Success 173502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1736760bc48eSAndrew Thompson struct usb_device * 1737a593f6b8SAndrew Thompson usb_alloc_device(device_t parent_dev, struct usb_bus *bus, 1738760bc48eSAndrew Thompson struct usb_device *parent_hub, uint8_t depth, uint8_t port_index, 17398d2dd5ddSAndrew Thompson uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode) 174002ac6454SAndrew Thompson { 1741760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1742760bc48eSAndrew Thompson struct usb_device *udev; 1743760bc48eSAndrew Thompson struct usb_device *adev; 1744760bc48eSAndrew Thompson struct usb_device *hub; 1745e0a69b51SAndrew Thompson usb_error_t err; 174602ac6454SAndrew Thompson uint8_t device_index; 17472a4c6157SAndrew Thompson uint8_t config_index; 17482a4c6157SAndrew Thompson uint8_t config_quirk; 17492a4c6157SAndrew Thompson uint8_t set_config_failed; 175002ac6454SAndrew Thompson 175102ac6454SAndrew Thompson DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1752f29a0724SAndrew Thompson "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n", 175302ac6454SAndrew Thompson parent_dev, bus, parent_hub, depth, port_index, port_no, 1754f29a0724SAndrew Thompson speed, mode); 175502ac6454SAndrew Thompson 175602ac6454SAndrew Thompson /* 175702ac6454SAndrew Thompson * Find an unused device index. In USB Host mode this is the 175802ac6454SAndrew Thompson * same as the device address. 175902ac6454SAndrew Thompson * 176002ac6454SAndrew Thompson * Device index zero is not used and device index 1 should 176102ac6454SAndrew Thompson * always be the root hub. 176202ac6454SAndrew Thompson */ 176302ac6454SAndrew Thompson for (device_index = USB_ROOT_HUB_ADDR; 176402ac6454SAndrew Thompson (device_index != bus->devices_max) && 176502ac6454SAndrew Thompson (bus->devices[device_index] != NULL); 176602ac6454SAndrew Thompson device_index++) /* nop */; 176702ac6454SAndrew Thompson 176802ac6454SAndrew Thompson if (device_index == bus->devices_max) { 176902ac6454SAndrew Thompson device_printf(bus->bdev, 1770767cb2e2SAndrew Thompson "No free USB device index for new device\n"); 177102ac6454SAndrew Thompson return (NULL); 177202ac6454SAndrew Thompson } 177302ac6454SAndrew Thompson 177402ac6454SAndrew Thompson if (depth > 0x10) { 177502ac6454SAndrew Thompson device_printf(bus->bdev, 1776767cb2e2SAndrew Thompson "Invalid device depth\n"); 177702ac6454SAndrew Thompson return (NULL); 177802ac6454SAndrew Thompson } 177902ac6454SAndrew Thompson udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 178094140f47SMark Johnston #if (USB_HAVE_MALLOC_WAITOK == 0) 178102ac6454SAndrew Thompson if (udev == NULL) { 178202ac6454SAndrew Thompson return (NULL); 178302ac6454SAndrew Thompson } 178494140f47SMark Johnston #endif 178502ac6454SAndrew Thompson /* initialise our SX-lock */ 178691cd9240SAndrew Thompson sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK); 1787a6b60150SHans Petter Selasky sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS); 178864cb5e2aSHans Petter Selasky sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK); 178902ac6454SAndrew Thompson 179091cd9240SAndrew Thompson cv_init(&udev->ctrlreq_cv, "WCTRL"); 179191cd9240SAndrew Thompson cv_init(&udev->ref_cv, "UGONE"); 179202ac6454SAndrew Thompson 179302ac6454SAndrew Thompson /* initialise our mutex */ 179491cd9240SAndrew Thompson mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF); 179502ac6454SAndrew Thompson 179602ac6454SAndrew Thompson /* initialise generic clear stall */ 1797a593f6b8SAndrew Thompson udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc; 179802ac6454SAndrew Thompson udev->cs_msg[0].udev = udev; 1799a593f6b8SAndrew Thompson udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc; 180002ac6454SAndrew Thompson udev->cs_msg[1].udev = udev; 180102ac6454SAndrew Thompson 180202ac6454SAndrew Thompson /* initialise some USB device fields */ 180302ac6454SAndrew Thompson udev->parent_hub = parent_hub; 180402ac6454SAndrew Thompson udev->parent_dev = parent_dev; 180502ac6454SAndrew Thompson udev->port_index = port_index; 180602ac6454SAndrew Thompson udev->port_no = port_no; 180702ac6454SAndrew Thompson udev->depth = depth; 180802ac6454SAndrew Thompson udev->bus = bus; 180902ac6454SAndrew Thompson udev->address = USB_START_ADDR; /* default value */ 1810e0a69b51SAndrew Thompson udev->plugtime = (usb_ticks_t)ticks; 181102ac6454SAndrew Thompson /* 181202ac6454SAndrew Thompson * We need to force the power mode to "on" because there are plenty 181302ac6454SAndrew Thompson * of USB devices out there that do not work very well with 181402ac6454SAndrew Thompson * automatic suspend and resume! 181502ac6454SAndrew Thompson */ 1816d2d71ce7SAndrew Thompson udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON); 181702ac6454SAndrew Thompson udev->pwr_save.last_xfer_time = ticks; 181802ac6454SAndrew Thompson /* we are not ready yet */ 181902ac6454SAndrew Thompson udev->refcount = 1; 182002ac6454SAndrew Thompson 182102ac6454SAndrew Thompson /* set up default endpoint descriptor */ 18225b3bb704SAndrew Thompson udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc); 18235b3bb704SAndrew Thompson udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT; 18245b3bb704SAndrew Thompson udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 18255b3bb704SAndrew Thompson udev->ctrl_ep_desc.bmAttributes = UE_CONTROL; 18265b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 18275b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[1] = 0; 18285b3bb704SAndrew Thompson udev->ctrl_ep_desc.bInterval = 0; 1829963169b4SHans Petter Selasky 1830963169b4SHans Petter Selasky /* set up default endpoint companion descriptor */ 1831963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc); 1832963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP; 1833963169b4SHans Petter Selasky 183402ac6454SAndrew Thompson udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 183502ac6454SAndrew Thompson 183602ac6454SAndrew Thompson udev->speed = speed; 1837f29a0724SAndrew Thompson udev->flags.usb_mode = mode; 183802ac6454SAndrew Thompson 183902ac6454SAndrew Thompson /* search for our High Speed USB HUB, if any */ 184002ac6454SAndrew Thompson 184102ac6454SAndrew Thompson adev = udev; 184202ac6454SAndrew Thompson hub = udev->parent_hub; 184302ac6454SAndrew Thompson 184402ac6454SAndrew Thompson while (hub) { 184502ac6454SAndrew Thompson if (hub->speed == USB_SPEED_HIGH) { 184602ac6454SAndrew Thompson udev->hs_hub_addr = hub->address; 1847495f25ceSAndrew Thompson udev->parent_hs_hub = hub; 184802ac6454SAndrew Thompson udev->hs_port_no = adev->port_no; 184902ac6454SAndrew Thompson break; 185002ac6454SAndrew Thompson } 185102ac6454SAndrew Thompson adev = hub; 185202ac6454SAndrew Thompson hub = hub->parent_hub; 185302ac6454SAndrew Thompson } 185402ac6454SAndrew Thompson 1855ae60fdfbSAndrew Thompson /* init the default endpoint */ 1856a593f6b8SAndrew Thompson usb_init_endpoint(udev, 0, 18575b3bb704SAndrew Thompson &udev->ctrl_ep_desc, 1858963169b4SHans Petter Selasky &udev->ctrl_ep_comp_desc, 18595b3bb704SAndrew Thompson &udev->ctrl_ep); 186002ac6454SAndrew Thompson 186102ac6454SAndrew Thompson /* set device index */ 186202ac6454SAndrew Thompson udev->device_index = device_index; 186302ac6454SAndrew Thompson 18648755859aSAndrew Thompson #if USB_HAVE_UGEN 18650f6e8f93SAndrew Thompson /* Create ugen name */ 1866ee3e3ff5SAndrew Thompson snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1867ee3e3ff5SAndrew Thompson USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1868ee3e3ff5SAndrew Thompson device_index); 18698755859aSAndrew Thompson LIST_INIT(&udev->pd_list); 18708755859aSAndrew Thompson 18710f6e8f93SAndrew Thompson /* Create the control endpoint device */ 18722ffd5fdcSHans Petter Selasky udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0, 18732ffd5fdcSHans Petter Selasky FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600); 18740f6e8f93SAndrew Thompson 18750f6e8f93SAndrew Thompson /* Create a link from /dev/ugenX.X to the default endpoint */ 18762ffd5fdcSHans Petter Selasky if (udev->ctrl_dev != NULL) 18772ffd5fdcSHans Petter Selasky make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name); 18780f6e8f93SAndrew Thompson #endif 1879963169b4SHans Petter Selasky /* Initialise device */ 1880963169b4SHans Petter Selasky if (bus->methods->device_init != NULL) { 1881963169b4SHans Petter Selasky err = (bus->methods->device_init) (udev); 1882963169b4SHans Petter Selasky if (err != 0) { 1883963169b4SHans Petter Selasky DPRINTFN(0, "device init %d failed " 1884963169b4SHans Petter Selasky "(%s, ignored)\n", device_index, 1885963169b4SHans Petter Selasky usbd_errstr(err)); 1886963169b4SHans Petter Selasky goto done; 1887963169b4SHans Petter Selasky } 1888963169b4SHans Petter Selasky } 1889963169b4SHans Petter Selasky /* set powered device state after device init is complete */ 1890963169b4SHans Petter Selasky usb_set_device_state(udev, USB_STATE_POWERED); 1891963169b4SHans Petter Selasky 1892f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 1893a593f6b8SAndrew Thompson err = usbd_req_set_address(udev, NULL, device_index); 189402ac6454SAndrew Thompson 1895963169b4SHans Petter Selasky /* 1896963169b4SHans Petter Selasky * This is the new USB device address from now on, if 1897963169b4SHans Petter Selasky * the set address request didn't set it already. 1898963169b4SHans Petter Selasky */ 1899963169b4SHans Petter Selasky if (udev->address == USB_START_ADDR) 190002ac6454SAndrew Thompson udev->address = device_index; 190102ac6454SAndrew Thompson 190202ac6454SAndrew Thompson /* 190302ac6454SAndrew Thompson * We ignore any set-address errors, hence there are 190402ac6454SAndrew Thompson * buggy USB devices out there that actually receive 190502ac6454SAndrew Thompson * the SETUP PID, but manage to set the address before 190602ac6454SAndrew Thompson * the STATUS stage is ACK'ed. If the device responds 190702ac6454SAndrew Thompson * to the subsequent get-descriptor at the new 190802ac6454SAndrew Thompson * address, then we know that the set-address command 190902ac6454SAndrew Thompson * was successful. 191002ac6454SAndrew Thompson */ 191102ac6454SAndrew Thompson if (err) { 191202ac6454SAndrew Thompson DPRINTFN(0, "set address %d failed " 191303797f33SAndrew Thompson "(%s, ignored)\n", udev->address, 1914a593f6b8SAndrew Thompson usbd_errstr(err)); 191502ac6454SAndrew Thompson } 191602ac6454SAndrew Thompson } else { 191702ac6454SAndrew Thompson /* We are not self powered */ 191802ac6454SAndrew Thompson udev->flags.self_powered = 0; 191902ac6454SAndrew Thompson 192002ac6454SAndrew Thompson /* Set unconfigured state */ 192102ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 192202ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 192302ac6454SAndrew Thompson 192402ac6454SAndrew Thompson /* Setup USB descriptors */ 1925a593f6b8SAndrew Thompson err = (usb_temp_setup_by_index_p) (udev, usb_template); 192602ac6454SAndrew Thompson if (err) { 19276be75992SEdward Tomasz Napierala DPRINTFN(0, "setting up USB template failed - " 19286be75992SEdward Tomasz Napierala "usb_template(4) not loaded?\n"); 192902ac6454SAndrew Thompson goto done; 193002ac6454SAndrew Thompson } 193102ac6454SAndrew Thompson } 1932a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 193302ac6454SAndrew Thompson 1934963169b4SHans Petter Selasky /* setup the device descriptor and the initial "wMaxPacketSize" */ 1935963169b4SHans Petter Selasky err = usbd_setup_device_desc(udev, NULL); 1936963169b4SHans Petter Selasky 1937963169b4SHans Petter Selasky if (err != 0) { 193823de050bSHans Petter Selasky /* try to enumerate two more times */ 1939a593f6b8SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 194023de050bSHans Petter Selasky if (err != 0) { 194123de050bSHans Petter Selasky err = usbd_req_re_enumerate(udev, NULL); 194223de050bSHans Petter Selasky if (err != 0) { 194302ac6454SAndrew Thompson goto done; 194402ac6454SAndrew Thompson } 194523de050bSHans Petter Selasky } 194623de050bSHans Petter Selasky } 194702ac6454SAndrew Thompson 194802ac6454SAndrew Thompson /* 194902ac6454SAndrew Thompson * Setup temporary USB attach args so that we can figure out some 195002ac6454SAndrew Thompson * basic quirks for this device. 195102ac6454SAndrew Thompson */ 1952a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 195302ac6454SAndrew Thompson 1954a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) { 195502ac6454SAndrew Thompson udev->flags.uq_bus_powered = 1; 195602ac6454SAndrew Thompson } 1957a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) { 195802ac6454SAndrew Thompson udev->flags.no_strings = 1; 195902ac6454SAndrew Thompson } 19606950c75fSHans Petter Selasky 1961f54ab96dSHans Petter Selasky usb_get_langid(udev); 19626950c75fSHans Petter Selasky 196302ac6454SAndrew Thompson /* assume 100mA bus powered for now. Changed when configured. */ 196402ac6454SAndrew Thompson udev->power = USB_MIN_POWER; 1965c86aead6SAndrew Thompson /* fetch the vendor and product strings from the device */ 1966f54ab96dSHans Petter Selasky usb_set_device_strings(udev); 196702ac6454SAndrew Thompson 19682a4c6157SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 19692a4c6157SAndrew Thompson /* USB device mode setup is complete */ 19702a4c6157SAndrew Thompson err = 0; 19712a4c6157SAndrew Thompson goto config_done; 19722a4c6157SAndrew Thompson } 197302ac6454SAndrew Thompson 197402ac6454SAndrew Thompson /* 197502ac6454SAndrew Thompson * Most USB devices should attach to config index 0 by 197602ac6454SAndrew Thompson * default 197702ac6454SAndrew Thompson */ 1978a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 197902ac6454SAndrew Thompson config_index = 0; 198002ac6454SAndrew Thompson config_quirk = 1; 1981a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 198202ac6454SAndrew Thompson config_index = 1; 198302ac6454SAndrew Thompson config_quirk = 1; 1984a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 198502ac6454SAndrew Thompson config_index = 2; 198602ac6454SAndrew Thompson config_quirk = 1; 1987a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 198802ac6454SAndrew Thompson config_index = 3; 198902ac6454SAndrew Thompson config_quirk = 1; 1990a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 199102ac6454SAndrew Thompson config_index = 4; 199202ac6454SAndrew Thompson config_quirk = 1; 199302ac6454SAndrew Thompson } else { 199402ac6454SAndrew Thompson config_index = 0; 199502ac6454SAndrew Thompson config_quirk = 0; 199602ac6454SAndrew Thompson } 199702ac6454SAndrew Thompson 19982a4c6157SAndrew Thompson set_config_failed = 0; 199902ac6454SAndrew Thompson repeat_set_config: 200002ac6454SAndrew Thompson 200102ac6454SAndrew Thompson DPRINTF("setting config %u\n", config_index); 200202ac6454SAndrew Thompson 200302ac6454SAndrew Thompson /* get the USB device configured */ 2004a593f6b8SAndrew Thompson err = usbd_set_config_index(udev, config_index); 200502ac6454SAndrew Thompson if (err) { 200602ac6454SAndrew Thompson if (udev->ddesc.bNumConfigurations != 0) { 200702ac6454SAndrew Thompson if (!set_config_failed) { 200802ac6454SAndrew Thompson set_config_failed = 1; 200902ac6454SAndrew Thompson /* XXX try to re-enumerate the device */ 20102a4c6157SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 201102ac6454SAndrew Thompson if (err == 0) 201202ac6454SAndrew Thompson goto repeat_set_config; 201302ac6454SAndrew Thompson } 20142a4c6157SAndrew Thompson DPRINTFN(0, "Failure selecting configuration index %u:" 20152a4c6157SAndrew Thompson "%s, port %u, addr %u (ignored)\n", 2016a593f6b8SAndrew Thompson config_index, usbd_errstr(err), udev->port_no, 201702ac6454SAndrew Thompson udev->address); 201802ac6454SAndrew Thompson } 201902ac6454SAndrew Thompson /* 20202a4c6157SAndrew Thompson * Some USB devices do not have any configurations. Ignore any 20212a4c6157SAndrew Thompson * set config failures! 202202ac6454SAndrew Thompson */ 202302ac6454SAndrew Thompson err = 0; 20242a4c6157SAndrew Thompson goto config_done; 20252a4c6157SAndrew Thompson } 20262a4c6157SAndrew Thompson if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) { 202702ac6454SAndrew Thompson if ((udev->cdesc->bNumInterface < 2) && 20282a4c6157SAndrew Thompson usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) { 20292a4c6157SAndrew Thompson DPRINTFN(0, "Found no endpoints, trying next config\n"); 203002ac6454SAndrew Thompson config_index++; 203102ac6454SAndrew Thompson goto repeat_set_config; 203202ac6454SAndrew Thompson } 203315d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 20343ee94386SKornel Duleba if (config_index == 0 && 20353ee94386SKornel Duleba usb_test_quirk(&uaa, UQ_MSC_NO_INQUIRY) == 0) { 203602ac6454SAndrew Thompson /* 203702ac6454SAndrew Thompson * Try to figure out if we have an 203802ac6454SAndrew Thompson * auto-install disk there: 203902ac6454SAndrew Thompson */ 2040d84a79e7SAndrew Thompson if (usb_iface_is_cdrom(udev, 0)) { 204102ac6454SAndrew Thompson DPRINTFN(0, "Found possible auto-install " 204202ac6454SAndrew Thompson "disk (trying next config)\n"); 204302ac6454SAndrew Thompson config_index++; 204402ac6454SAndrew Thompson goto repeat_set_config; 204502ac6454SAndrew Thompson } 204602ac6454SAndrew Thompson } 204715d4edd3SHans Petter Selasky #endif 20482a4c6157SAndrew Thompson } 204915d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 2050d46dc4adSHans Petter Selasky if (set_config_failed == 0 && config_index == 0 && 20517520b888SHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_START_STOP) == 0 && 20527520b888SHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_PREVENT_ALLOW) == 0 && 20539157ad4bSHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 && 20547520b888SHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_TEST_UNIT_READY) == 0 && 20553ee94386SKornel Duleba usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0 && 20563ee94386SKornel Duleba usb_test_quirk(&uaa, UQ_MSC_NO_INQUIRY) == 0) { 2057d46dc4adSHans Petter Selasky /* 2058d46dc4adSHans Petter Selasky * Try to figure out if there are any MSC quirks we 2059d46dc4adSHans Petter Selasky * should apply automatically: 2060d46dc4adSHans Petter Selasky */ 20617520b888SHans Petter Selasky err = usb_msc_auto_quirk(udev, 0, &uaa); 2062d46dc4adSHans Petter Selasky 2063d46dc4adSHans Petter Selasky if (err != 0) { 2064d46dc4adSHans Petter Selasky set_config_failed = 1; 2065d46dc4adSHans Petter Selasky goto repeat_set_config; 2066d46dc4adSHans Petter Selasky } 2067d46dc4adSHans Petter Selasky } 206815d4edd3SHans Petter Selasky #endif 206902ac6454SAndrew Thompson 20702a4c6157SAndrew Thompson config_done: 207102ac6454SAndrew Thompson DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 207202ac6454SAndrew Thompson udev->address, udev, udev->parent_hub); 207302ac6454SAndrew Thompson 207402ac6454SAndrew Thompson /* register our device - we are ready */ 2075a593f6b8SAndrew Thompson usb_bus_port_set_device(bus, parent_hub ? 207602ac6454SAndrew Thompson parent_hub->hub->ports + port_index : NULL, udev, device_index); 207702ac6454SAndrew Thompson 20780f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 20798755859aSAndrew Thompson /* Symlink the ugen device name */ 2080a593f6b8SAndrew Thompson udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); 20818755859aSAndrew Thompson 20828755859aSAndrew Thompson /* Announce device */ 208388f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s\n", udev->ugen_name, 208488f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 208502ac6454SAndrew Thompson device_get_nameunit(udev->bus->bdev)); 20868bb77249SHans Petter Selasky #endif 20877efaaa9aSAndrew Thompson 20888bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 20893acd904dSAndrew Thompson usb_notify_addq("ATTACH", udev); 20908755859aSAndrew Thompson #endif 209102ac6454SAndrew Thompson done: 209202ac6454SAndrew Thompson if (err) { 2093d88688c7SAndrew Thompson /* 2094d88688c7SAndrew Thompson * Free USB device and all subdevices, if any. 2095d88688c7SAndrew Thompson */ 2096d88688c7SAndrew Thompson usb_free_device(udev, 0); 209702ac6454SAndrew Thompson udev = NULL; 209802ac6454SAndrew Thompson } 209902ac6454SAndrew Thompson return (udev); 210002ac6454SAndrew Thompson } 210102ac6454SAndrew Thompson 21020f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 21032ffd5fdcSHans Petter Selasky struct usb_fs_privdata * 21042ffd5fdcSHans Petter Selasky usb_make_dev(struct usb_device *udev, const char *devname, int ep, 21052ffd5fdcSHans Petter Selasky int fi, int rwmode, uid_t uid, gid_t gid, int mode) 2106ee3e3ff5SAndrew Thompson { 2107760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2108ac3490fdSHans Petter Selasky struct make_dev_args args; 21092ffd5fdcSHans Petter Selasky char buffer[32]; 2110ee3e3ff5SAndrew Thompson 2111ee3e3ff5SAndrew Thompson /* Store information to locate ourselves again later */ 2112760bc48eSAndrew Thompson pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, 2113ee3e3ff5SAndrew Thompson M_WAITOK | M_ZERO); 2114ee3e3ff5SAndrew Thompson pd->bus_index = device_get_unit(udev->bus->bdev); 2115ee3e3ff5SAndrew Thompson pd->dev_index = udev->device_index; 2116ee3e3ff5SAndrew Thompson pd->ep_addr = ep; 21172ffd5fdcSHans Petter Selasky pd->fifo_index = fi; 21182ffd5fdcSHans Petter Selasky pd->mode = rwmode; 2119ee3e3ff5SAndrew Thompson 2120ee3e3ff5SAndrew Thompson /* Now, create the device itself */ 21212ffd5fdcSHans Petter Selasky if (devname == NULL) { 21222ffd5fdcSHans Petter Selasky devname = buffer; 21232ffd5fdcSHans Petter Selasky snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u", 2124f35aaff0SAndrew Thompson pd->bus_index, pd->dev_index, pd->ep_addr); 21252ffd5fdcSHans Petter Selasky } 21262ffd5fdcSHans Petter Selasky 2127ac3490fdSHans Petter Selasky /* Setup arguments for make_dev_s() */ 2128ac3490fdSHans Petter Selasky make_dev_args_init(&args); 2129ac3490fdSHans Petter Selasky args.mda_devsw = &usb_devsw; 2130ac3490fdSHans Petter Selasky args.mda_uid = uid; 2131ac3490fdSHans Petter Selasky args.mda_gid = gid; 2132ac3490fdSHans Petter Selasky args.mda_mode = mode; 2133ac3490fdSHans Petter Selasky args.mda_si_drv1 = pd; 21342ffd5fdcSHans Petter Selasky 2135ac3490fdSHans Petter Selasky if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) { 21362ffd5fdcSHans Petter Selasky DPRINTFN(0, "Failed to create device %s\n", devname); 21372ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 21382ffd5fdcSHans Petter Selasky return (NULL); 21392ffd5fdcSHans Petter Selasky } 21402ffd5fdcSHans Petter Selasky return (pd); 21412ffd5fdcSHans Petter Selasky } 21422ffd5fdcSHans Petter Selasky 21432ffd5fdcSHans Petter Selasky void 2144b78e84d1SHans Petter Selasky usb_destroy_dev_sync(struct usb_fs_privdata *pd) 21452ffd5fdcSHans Petter Selasky { 2146b78e84d1SHans Petter Selasky DPRINTFN(1, "Destroying device at ugen%d.%d\n", 2147b78e84d1SHans Petter Selasky pd->bus_index, pd->dev_index); 21482ffd5fdcSHans Petter Selasky 2149b78e84d1SHans Petter Selasky /* 2150b78e84d1SHans Petter Selasky * Destroy character device synchronously. After this 2151b78e84d1SHans Petter Selasky * all system calls are returned. Can block. 2152b78e84d1SHans Petter Selasky */ 21532ffd5fdcSHans Petter Selasky destroy_dev(pd->cdev); 21542ffd5fdcSHans Petter Selasky 21552ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 2156ee3e3ff5SAndrew Thompson } 2157ee3e3ff5SAndrew Thompson 2158b78e84d1SHans Petter Selasky void 2159b78e84d1SHans Petter Selasky usb_destroy_dev(struct usb_fs_privdata *pd) 2160b78e84d1SHans Petter Selasky { 2161b78e84d1SHans Petter Selasky struct usb_bus *bus; 2162b78e84d1SHans Petter Selasky 2163b78e84d1SHans Petter Selasky if (pd == NULL) 2164b78e84d1SHans Petter Selasky return; 2165b78e84d1SHans Petter Selasky 2166b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 2167b78e84d1SHans Petter Selasky bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index); 2168b78e84d1SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2169b78e84d1SHans Petter Selasky 2170b78e84d1SHans Petter Selasky if (bus == NULL) { 2171b78e84d1SHans Petter Selasky usb_destroy_dev_sync(pd); 2172b78e84d1SHans Petter Selasky return; 2173b78e84d1SHans Petter Selasky } 2174b78e84d1SHans Petter Selasky 217507dbde67SHans Petter Selasky /* make sure we can re-use the device name */ 217607dbde67SHans Petter Selasky delist_dev(pd->cdev); 217707dbde67SHans Petter Selasky 2178b78e84d1SHans Petter Selasky USB_BUS_LOCK(bus); 2179b78e84d1SHans Petter Selasky LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next); 2180b78e84d1SHans Petter Selasky /* get cleanup going */ 2181b78e84d1SHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2182b78e84d1SHans Petter Selasky &bus->cleanup_msg[0], &bus->cleanup_msg[1]); 2183b78e84d1SHans Petter Selasky USB_BUS_UNLOCK(bus); 2184b78e84d1SHans Petter Selasky } 2185b78e84d1SHans Petter Selasky 2186ee3e3ff5SAndrew Thompson static void 2187a593f6b8SAndrew Thompson usb_cdev_create(struct usb_device *udev) 2188ee3e3ff5SAndrew Thompson { 2189760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2190760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 2191760bc48eSAndrew Thompson struct usb_descriptor *desc; 2192760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2193f5f145baSAndrew Thompson int inmode, outmode, inmask, outmask, mode; 2194f5f145baSAndrew Thompson uint8_t ep; 2195ee3e3ff5SAndrew Thompson 2196ee3e3ff5SAndrew Thompson KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 2197ee3e3ff5SAndrew Thompson 2198ee3e3ff5SAndrew Thompson DPRINTFN(2, "Creating device nodes\n"); 2199ee3e3ff5SAndrew Thompson 2200a593f6b8SAndrew Thompson if (usbd_get_mode(udev) == USB_MODE_DEVICE) { 2201ee3e3ff5SAndrew Thompson inmode = FWRITE; 2202ee3e3ff5SAndrew Thompson outmode = FREAD; 2203ee3e3ff5SAndrew Thompson } else { /* USB_MODE_HOST */ 2204ee3e3ff5SAndrew Thompson inmode = FREAD; 2205ee3e3ff5SAndrew Thompson outmode = FWRITE; 2206ee3e3ff5SAndrew Thompson } 2207ee3e3ff5SAndrew Thompson 2208f5f145baSAndrew Thompson inmask = 0; 2209f5f145baSAndrew Thompson outmask = 0; 2210f5f145baSAndrew Thompson desc = NULL; 2211f5f145baSAndrew Thompson 2212f5f145baSAndrew Thompson /* 2213f5f145baSAndrew Thompson * Collect all used endpoint numbers instead of just 2214f5f145baSAndrew Thompson * generating 16 static endpoints. 2215f5f145baSAndrew Thompson */ 2216a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 2217a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 2218f5f145baSAndrew Thompson /* filter out all endpoint descriptors */ 2219f5f145baSAndrew Thompson if ((desc->bDescriptorType == UDESC_ENDPOINT) && 2220f5f145baSAndrew Thompson (desc->bLength >= sizeof(*ed))) { 2221760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)desc; 2222f5f145baSAndrew Thompson 2223f5f145baSAndrew Thompson /* update masks */ 2224f5f145baSAndrew Thompson ep = ed->bEndpointAddress; 2225f5f145baSAndrew Thompson if (UE_GET_DIR(ep) == UE_DIR_OUT) 2226f5f145baSAndrew Thompson outmask |= 1 << UE_GET_ADDR(ep); 2227f5f145baSAndrew Thompson else 2228f5f145baSAndrew Thompson inmask |= 1 << UE_GET_ADDR(ep); 2229f5f145baSAndrew Thompson } 2230f5f145baSAndrew Thompson } 2231ee3e3ff5SAndrew Thompson 2232ee3e3ff5SAndrew Thompson /* Create all available endpoints except EP0 */ 2233ee3e3ff5SAndrew Thompson for (ep = 1; ep < 16; ep++) { 22342ffd5fdcSHans Petter Selasky mode = (inmask & (1 << ep)) ? inmode : 0; 22352ffd5fdcSHans Petter Selasky mode |= (outmask & (1 << ep)) ? outmode : 0; 2236ee3e3ff5SAndrew Thompson if (mode == 0) 2237ee3e3ff5SAndrew Thompson continue; /* no IN or OUT endpoint */ 2238ee3e3ff5SAndrew Thompson 22392ffd5fdcSHans Petter Selasky pd = usb_make_dev(udev, NULL, ep, 0, 22402ffd5fdcSHans Petter Selasky mode, UID_ROOT, GID_OPERATOR, 0600); 22412ffd5fdcSHans Petter Selasky 22422ffd5fdcSHans Petter Selasky if (pd != NULL) 2243ee3e3ff5SAndrew Thompson LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 2244ee3e3ff5SAndrew Thompson } 2245ee3e3ff5SAndrew Thompson } 2246ee3e3ff5SAndrew Thompson 2247ee3e3ff5SAndrew Thompson static void 2248a593f6b8SAndrew Thompson usb_cdev_free(struct usb_device *udev) 2249ee3e3ff5SAndrew Thompson { 2250760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2251ee3e3ff5SAndrew Thompson 2252ee3e3ff5SAndrew Thompson DPRINTFN(2, "Freeing device nodes\n"); 2253ee3e3ff5SAndrew Thompson 2254ee3e3ff5SAndrew Thompson while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 2255ee3e3ff5SAndrew Thompson KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 2256ee3e3ff5SAndrew Thompson 2257ee3e3ff5SAndrew Thompson LIST_REMOVE(pd, pd_next); 2258ee3e3ff5SAndrew Thompson 22592ffd5fdcSHans Petter Selasky usb_destroy_dev(pd); 22602ffd5fdcSHans Petter Selasky } 2261ee3e3ff5SAndrew Thompson } 22620f6e8f93SAndrew Thompson #endif 2263ee3e3ff5SAndrew Thompson 226402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2265a593f6b8SAndrew Thompson * usb_free_device 226602ac6454SAndrew Thompson * 2267d88688c7SAndrew Thompson * This function is NULL safe and will free an USB device and its 2268d88688c7SAndrew Thompson * children devices, if any. 2269bdd41206SAndrew Thompson * 2270d88688c7SAndrew Thompson * Flag values: Reserved, set to zero. 227102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 227202ac6454SAndrew Thompson void 2273a593f6b8SAndrew Thompson usb_free_device(struct usb_device *udev, uint8_t flag) 227402ac6454SAndrew Thompson { 2275760bc48eSAndrew Thompson struct usb_bus *bus; 2276bdd41206SAndrew Thompson 2277bdd41206SAndrew Thompson if (udev == NULL) 2278bdd41206SAndrew Thompson return; /* already freed */ 227902ac6454SAndrew Thompson 228002ac6454SAndrew Thompson DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 228102ac6454SAndrew Thompson 2282bd216778SAndrew Thompson bus = udev->bus; 2283f97da128SHans Petter Selasky 2284f97da128SHans Petter Selasky /* set DETACHED state to prevent any further references */ 2285a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_DETACHED); 2286bdd41206SAndrew Thompson 22878bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 22883acd904dSAndrew Thompson usb_notify_addq("DETACH", udev); 22898bb77249SHans Petter Selasky #endif 229002ac6454SAndrew Thompson 22918bb77249SHans Petter Selasky #if USB_HAVE_UGEN 2292cabe79d9SMarius Strobl if (!rebooting) { 229388f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name, 229488f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 229588f9a4ffSEdward Tomasz Napierala device_get_nameunit(bus->bdev)); 2296cabe79d9SMarius Strobl } 229702ac6454SAndrew Thompson 2298ee3e3ff5SAndrew Thompson /* Destroy UGEN symlink, if any */ 229902ac6454SAndrew Thompson if (udev->ugen_symlink) { 2300a593f6b8SAndrew Thompson usb_free_symlink(udev->ugen_symlink); 230102ac6454SAndrew Thompson udev->ugen_symlink = NULL; 230202ac6454SAndrew Thompson } 230302ac6454SAndrew Thompson 23042ffd5fdcSHans Petter Selasky usb_destroy_dev(udev->ctrl_dev); 23050f6e8f93SAndrew Thompson #endif 230602ac6454SAndrew Thompson 2307f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 230802ac6454SAndrew Thompson /* stop receiving any control transfers (Device Side Mode) */ 23095b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 231002ac6454SAndrew Thompson } 231102ac6454SAndrew Thompson 2312bdd41206SAndrew Thompson /* the following will get the device unconfigured in software */ 2313d88688c7SAndrew Thompson usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0); 231402ac6454SAndrew Thompson 2315f97da128SHans Petter Selasky /* final device unregister after all character devices are closed */ 2316f97da128SHans Petter Selasky usb_bus_port_set_device(bus, udev->parent_hub ? 2317f97da128SHans Petter Selasky udev->parent_hub->hub->ports + udev->port_index : NULL, 2318f97da128SHans Petter Selasky NULL, USB_ROOT_HUB_ADDR); 2319f97da128SHans Petter Selasky 232002ac6454SAndrew Thompson /* unsetup any leftover default USB transfers */ 23215b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 232202ac6454SAndrew Thompson 232302ac6454SAndrew Thompson /* template unsetup, if any */ 2324a593f6b8SAndrew Thompson (usb_temp_unsetup_p) (udev); 232502ac6454SAndrew Thompson 232602ac6454SAndrew Thompson /* 232702ac6454SAndrew Thompson * Make sure that our clear-stall messages are not queued 232802ac6454SAndrew Thompson * anywhere: 232902ac6454SAndrew Thompson */ 233002ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 233143ea03d7SHans Petter Selasky usb_proc_mwait(USB_BUS_CS_PROC(udev->bus), 233202ac6454SAndrew Thompson &udev->cs_msg[0], &udev->cs_msg[1]); 233302ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 233402ac6454SAndrew Thompson 2335b78e84d1SHans Petter Selasky /* wait for all references to go away */ 2336b78e84d1SHans Petter Selasky usb_wait_pending_refs(udev); 2337b78e84d1SHans Petter Selasky 233891cd9240SAndrew Thompson sx_destroy(&udev->enum_sx); 23392df1e9a6SAndrew Thompson sx_destroy(&udev->sr_sx); 234064cb5e2aSHans Petter Selasky sx_destroy(&udev->ctrl_sx); 234102ac6454SAndrew Thompson 234291cd9240SAndrew Thompson cv_destroy(&udev->ctrlreq_cv); 234391cd9240SAndrew Thompson cv_destroy(&udev->ref_cv); 234402ac6454SAndrew Thompson 234591cd9240SAndrew Thompson mtx_destroy(&udev->device_mtx); 23468755859aSAndrew Thompson #if USB_HAVE_UGEN 2347ee3e3ff5SAndrew Thompson KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 23488755859aSAndrew Thompson #endif 234902ac6454SAndrew Thompson 2350963169b4SHans Petter Selasky /* Uninitialise device */ 2351963169b4SHans Petter Selasky if (bus->methods->device_uninit != NULL) 2352963169b4SHans Petter Selasky (bus->methods->device_uninit) (udev); 2353963169b4SHans Petter Selasky 235402ac6454SAndrew Thompson /* free device */ 235528212de2SAndrew Thompson free(udev->serial, M_USB); 235628212de2SAndrew Thompson free(udev->manufacturer, M_USB); 235728212de2SAndrew Thompson free(udev->product, M_USB); 235802ac6454SAndrew Thompson free(udev, M_USB); 235902ac6454SAndrew Thompson } 236002ac6454SAndrew Thompson 236102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2362a593f6b8SAndrew Thompson * usbd_get_iface 236302ac6454SAndrew Thompson * 236402ac6454SAndrew Thompson * This function is the safe way to get the USB interface structure 236502ac6454SAndrew Thompson * pointer by interface index. 236602ac6454SAndrew Thompson * 236702ac6454SAndrew Thompson * Return values: 236802ac6454SAndrew Thompson * NULL: Interface not present. 236902ac6454SAndrew Thompson * Else: Pointer to USB interface structure. 237002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 2371760bc48eSAndrew Thompson struct usb_interface * 2372a593f6b8SAndrew Thompson usbd_get_iface(struct usb_device *udev, uint8_t iface_index) 237302ac6454SAndrew Thompson { 2374760bc48eSAndrew Thompson struct usb_interface *iface = udev->ifaces + iface_index; 237502ac6454SAndrew Thompson 2376bdd41206SAndrew Thompson if (iface_index >= udev->ifaces_max) 237702ac6454SAndrew Thompson return (NULL); 237802ac6454SAndrew Thompson return (iface); 237902ac6454SAndrew Thompson } 238002ac6454SAndrew Thompson 238102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 23823c8d24f4SAndrew Thompson * usbd_find_descriptor 238302ac6454SAndrew Thompson * 238402ac6454SAndrew Thompson * This function will lookup the first descriptor that matches the 238502ac6454SAndrew Thompson * criteria given by the arguments "type" and "subtype". Descriptors 238602ac6454SAndrew Thompson * will only be searched within the interface having the index 238702ac6454SAndrew Thompson * "iface_index". If the "id" argument points to an USB descriptor, 238802ac6454SAndrew Thompson * it will be skipped before the search is started. This allows 238902ac6454SAndrew Thompson * searching for multiple descriptors using the same criteria. Else 239002ac6454SAndrew Thompson * the search is started after the interface descriptor. 239102ac6454SAndrew Thompson * 239202ac6454SAndrew Thompson * Return values: 239302ac6454SAndrew Thompson * NULL: End of descriptors 239402ac6454SAndrew Thompson * Else: A descriptor matching the criteria 239502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 239602ac6454SAndrew Thompson void * 23973c8d24f4SAndrew Thompson usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index, 239802ac6454SAndrew Thompson uint8_t type, uint8_t type_mask, 239902ac6454SAndrew Thompson uint8_t subtype, uint8_t subtype_mask) 240002ac6454SAndrew Thompson { 2401760bc48eSAndrew Thompson struct usb_descriptor *desc; 2402760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2403760bc48eSAndrew Thompson struct usb_interface *iface; 240402ac6454SAndrew Thompson 2405a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 240602ac6454SAndrew Thompson if (cd == NULL) { 240702ac6454SAndrew Thompson return (NULL); 240802ac6454SAndrew Thompson } 240902ac6454SAndrew Thompson if (id == NULL) { 2410a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, iface_index); 241102ac6454SAndrew Thompson if (iface == NULL) { 241202ac6454SAndrew Thompson return (NULL); 241302ac6454SAndrew Thompson } 2414a593f6b8SAndrew Thompson id = usbd_get_interface_descriptor(iface); 241502ac6454SAndrew Thompson if (id == NULL) { 241602ac6454SAndrew Thompson return (NULL); 241702ac6454SAndrew Thompson } 241802ac6454SAndrew Thompson } 241902ac6454SAndrew Thompson desc = (void *)id; 242002ac6454SAndrew Thompson 2421a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 242202ac6454SAndrew Thompson if (desc->bDescriptorType == UDESC_INTERFACE) { 242302ac6454SAndrew Thompson break; 242402ac6454SAndrew Thompson } 242502ac6454SAndrew Thompson if (((desc->bDescriptorType & type_mask) == type) && 242602ac6454SAndrew Thompson ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 242702ac6454SAndrew Thompson return (desc); 242802ac6454SAndrew Thompson } 242902ac6454SAndrew Thompson } 243002ac6454SAndrew Thompson return (NULL); 243102ac6454SAndrew Thompson } 243202ac6454SAndrew Thompson 243302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2434a593f6b8SAndrew Thompson * usb_devinfo 243502ac6454SAndrew Thompson * 243602ac6454SAndrew Thompson * This function will dump information from the device descriptor 243702ac6454SAndrew Thompson * belonging to the USB device pointed to by "udev", to the string 243802ac6454SAndrew Thompson * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 243902ac6454SAndrew Thompson * including the terminating zero. 244002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 244102ac6454SAndrew Thompson void 2442a593f6b8SAndrew Thompson usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) 244302ac6454SAndrew Thompson { 2444760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 244502ac6454SAndrew Thompson uint16_t bcdDevice; 244602ac6454SAndrew Thompson uint16_t bcdUSB; 244702ac6454SAndrew Thompson 244802ac6454SAndrew Thompson bcdUSB = UGETW(udd->bcdUSB); 244902ac6454SAndrew Thompson bcdDevice = UGETW(udd->bcdDevice); 245002ac6454SAndrew Thompson 245102ac6454SAndrew Thompson if (udd->bDeviceClass != 0xFF) { 245202ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 24538755859aSAndrew Thompson "%x.%02x, addr %d", 2454ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2455ae538d85SAndrew Thompson usb_get_product(udev), 245602ac6454SAndrew Thompson udd->bDeviceClass, udd->bDeviceSubClass, 245702ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 245802ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 245902ac6454SAndrew Thompson udev->address); 246002ac6454SAndrew Thompson } else { 246102ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 24628755859aSAndrew Thompson "%x.%02x, addr %d", 2463ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2464ae538d85SAndrew Thompson usb_get_product(udev), 246502ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 246602ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 246702ac6454SAndrew Thompson udev->address); 246802ac6454SAndrew Thompson } 246902ac6454SAndrew Thompson } 247002ac6454SAndrew Thompson 2471ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 247202ac6454SAndrew Thompson /* 247302ac6454SAndrew Thompson * Descriptions of of known vendors and devices ("products"). 247402ac6454SAndrew Thompson */ 247502ac6454SAndrew Thompson struct usb_knowndev { 247602ac6454SAndrew Thompson uint16_t vendor; 247702ac6454SAndrew Thompson uint16_t product; 247802ac6454SAndrew Thompson uint32_t flags; 247902ac6454SAndrew Thompson const char *vendorname; 248002ac6454SAndrew Thompson const char *productname; 248102ac6454SAndrew Thompson }; 248202ac6454SAndrew Thompson 248302ac6454SAndrew Thompson #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 248402ac6454SAndrew Thompson 248502ac6454SAndrew Thompson #include "usbdevs.h" 248602ac6454SAndrew Thompson #include "usbdevs_data.h" 248702ac6454SAndrew Thompson #endif /* USB_VERBOSE */ 248802ac6454SAndrew Thompson 2489f54ab96dSHans Petter Selasky void 2490f54ab96dSHans Petter Selasky usb_set_device_strings(struct usb_device *udev) 249102ac6454SAndrew Thompson { 2492760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 2493ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 249402ac6454SAndrew Thompson const struct usb_knowndev *kdp; 249502ac6454SAndrew Thompson #endif 24962df1e9a6SAndrew Thompson char *temp_ptr; 24977a918edfSAndrew Thompson size_t temp_size; 249802ac6454SAndrew Thompson uint16_t vendor_id; 249902ac6454SAndrew Thompson uint16_t product_id; 25006950c75fSHans Petter Selasky uint8_t do_unlock; 250102ac6454SAndrew Thompson 25026950c75fSHans Petter Selasky /* Protect scratch area */ 250364cb5e2aSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 25046950c75fSHans Petter Selasky 25056950c75fSHans Petter Selasky temp_ptr = (char *)udev->scratch.data; 25066950c75fSHans Petter Selasky temp_size = sizeof(udev->scratch.data); 25077a918edfSAndrew Thompson 250802ac6454SAndrew Thompson vendor_id = UGETW(udd->idVendor); 250902ac6454SAndrew Thompson product_id = UGETW(udd->idProduct); 251002ac6454SAndrew Thompson 2511f54ab96dSHans Petter Selasky /* cleanup old strings, if any */ 2512f54ab96dSHans Petter Selasky free(udev->serial, M_USB); 2513f54ab96dSHans Petter Selasky free(udev->manufacturer, M_USB); 2514f54ab96dSHans Petter Selasky free(udev->product, M_USB); 2515f54ab96dSHans Petter Selasky 2516f54ab96dSHans Petter Selasky /* zero the string pointers */ 2517f54ab96dSHans Petter Selasky udev->serial = NULL; 2518f54ab96dSHans Petter Selasky udev->manufacturer = NULL; 2519f54ab96dSHans Petter Selasky udev->product = NULL; 2520f54ab96dSHans Petter Selasky 2521c86aead6SAndrew Thompson /* get serial number string */ 25227a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2523c86aead6SAndrew Thompson udev->ddesc.iSerialNumber); 25247a918edfSAndrew Thompson udev->serial = strdup(temp_ptr, M_USB); 252502ac6454SAndrew Thompson 2526c86aead6SAndrew Thompson /* get manufacturer string */ 25277a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2528c86aead6SAndrew Thompson udev->ddesc.iManufacturer); 25297a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 25307a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 25317a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 2532c86aead6SAndrew Thompson 2533c86aead6SAndrew Thompson /* get product string */ 25347a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2535c86aead6SAndrew Thompson udev->ddesc.iProduct); 25367a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 25377a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 25387a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 2539c86aead6SAndrew Thompson 2540ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 2541c86aead6SAndrew Thompson if (udev->manufacturer == NULL || udev->product == NULL) { 2542c86aead6SAndrew Thompson for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) { 254302ac6454SAndrew Thompson if (kdp->vendor == vendor_id && 254402ac6454SAndrew Thompson (kdp->product == product_id || 254502ac6454SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 254602ac6454SAndrew Thompson break; 254702ac6454SAndrew Thompson } 254802ac6454SAndrew Thompson if (kdp->vendorname != NULL) { 2549c86aead6SAndrew Thompson /* XXX should use pointer to knowndevs string */ 2550c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 2551c86aead6SAndrew Thompson udev->manufacturer = strdup(kdp->vendorname, 2552c86aead6SAndrew Thompson M_USB); 2553c86aead6SAndrew Thompson } 2554c86aead6SAndrew Thompson if (udev->product == NULL && 2555c86aead6SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) { 2556c86aead6SAndrew Thompson udev->product = strdup(kdp->productname, 2557c86aead6SAndrew Thompson M_USB); 2558c86aead6SAndrew Thompson } 255902ac6454SAndrew Thompson } 256002ac6454SAndrew Thompson } 256102ac6454SAndrew Thompson #endif 2562c86aead6SAndrew Thompson /* Provide default strings if none were found */ 2563c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 25647a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id); 25657a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 256602ac6454SAndrew Thompson } 2567c86aead6SAndrew Thompson if (udev->product == NULL) { 25687a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "product 0x%04x", product_id); 25697a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 257002ac6454SAndrew Thompson } 25716950c75fSHans Petter Selasky 25726950c75fSHans Petter Selasky if (do_unlock) 257364cb5e2aSHans Petter Selasky usbd_ctrl_unlock(udev); 257402ac6454SAndrew Thompson } 257502ac6454SAndrew Thompson 257602ac6454SAndrew Thompson /* 257702ac6454SAndrew Thompson * Returns: 257802ac6454SAndrew Thompson * See: USB_MODE_XXX 257902ac6454SAndrew Thompson */ 2580f29a0724SAndrew Thompson enum usb_hc_mode 2581a593f6b8SAndrew Thompson usbd_get_mode(struct usb_device *udev) 258202ac6454SAndrew Thompson { 2583f29a0724SAndrew Thompson return (udev->flags.usb_mode); 258402ac6454SAndrew Thompson } 258502ac6454SAndrew Thompson 258602ac6454SAndrew Thompson /* 258702ac6454SAndrew Thompson * Returns: 258802ac6454SAndrew Thompson * See: USB_SPEED_XXX 258902ac6454SAndrew Thompson */ 25908d2dd5ddSAndrew Thompson enum usb_dev_speed 2591a593f6b8SAndrew Thompson usbd_get_speed(struct usb_device *udev) 259202ac6454SAndrew Thompson { 259302ac6454SAndrew Thompson return (udev->speed); 259402ac6454SAndrew Thompson } 259502ac6454SAndrew Thompson 259602ac6454SAndrew Thompson uint32_t 2597a593f6b8SAndrew Thompson usbd_get_isoc_fps(struct usb_device *udev) 259802ac6454SAndrew Thompson { 259902ac6454SAndrew Thompson ; /* indent fix */ 260002ac6454SAndrew Thompson switch (udev->speed) { 260102ac6454SAndrew Thompson case USB_SPEED_LOW: 260202ac6454SAndrew Thompson case USB_SPEED_FULL: 260302ac6454SAndrew Thompson return (1000); 260402ac6454SAndrew Thompson default: 260502ac6454SAndrew Thompson return (8000); 260602ac6454SAndrew Thompson } 260702ac6454SAndrew Thompson } 260802ac6454SAndrew Thompson 2609760bc48eSAndrew Thompson struct usb_device_descriptor * 2610a593f6b8SAndrew Thompson usbd_get_device_descriptor(struct usb_device *udev) 261102ac6454SAndrew Thompson { 261202ac6454SAndrew Thompson if (udev == NULL) 261302ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 261402ac6454SAndrew Thompson return (&udev->ddesc); 261502ac6454SAndrew Thompson } 261602ac6454SAndrew Thompson 2617760bc48eSAndrew Thompson struct usb_config_descriptor * 2618a593f6b8SAndrew Thompson usbd_get_config_descriptor(struct usb_device *udev) 261902ac6454SAndrew Thompson { 262002ac6454SAndrew Thompson if (udev == NULL) 262102ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 262202ac6454SAndrew Thompson return (udev->cdesc); 262302ac6454SAndrew Thompson } 262402ac6454SAndrew Thompson 262502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2626a593f6b8SAndrew Thompson * usb_test_quirk - test a device for a given quirk 262702ac6454SAndrew Thompson * 262802ac6454SAndrew Thompson * Return values: 262902ac6454SAndrew Thompson * 0: The USB device does not have the given quirk. 263002ac6454SAndrew Thompson * Else: The USB device has the given quirk. 263102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 263202ac6454SAndrew Thompson uint8_t 2633a593f6b8SAndrew Thompson usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk) 263402ac6454SAndrew Thompson { 263502ac6454SAndrew Thompson uint8_t found; 26363f37fb62SHans Petter Selasky uint8_t x; 26373f37fb62SHans Petter Selasky 26383f37fb62SHans Petter Selasky if (quirk == UQ_NONE) 26393f37fb62SHans Petter Selasky return (0); 26403f37fb62SHans Petter Selasky 26413f37fb62SHans Petter Selasky /* search the automatic per device quirks first */ 26423f37fb62SHans Petter Selasky 26433f37fb62SHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 26443f37fb62SHans Petter Selasky if (uaa->device->autoQuirk[x] == quirk) 26453f37fb62SHans Petter Selasky return (1); 26463f37fb62SHans Petter Selasky } 26473f37fb62SHans Petter Selasky 26483f37fb62SHans Petter Selasky /* search global quirk table, if any */ 264902ac6454SAndrew Thompson 2650a593f6b8SAndrew Thompson found = (usb_test_quirk_p) (&uaa->info, quirk); 26513f37fb62SHans Petter Selasky 265202ac6454SAndrew Thompson return (found); 265302ac6454SAndrew Thompson } 265402ac6454SAndrew Thompson 2655760bc48eSAndrew Thompson struct usb_interface_descriptor * 2656a593f6b8SAndrew Thompson usbd_get_interface_descriptor(struct usb_interface *iface) 265702ac6454SAndrew Thompson { 265802ac6454SAndrew Thompson if (iface == NULL) 265902ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 266002ac6454SAndrew Thompson return (iface->idesc); 266102ac6454SAndrew Thompson } 266202ac6454SAndrew Thompson 266302ac6454SAndrew Thompson uint8_t 2664a593f6b8SAndrew Thompson usbd_get_interface_altindex(struct usb_interface *iface) 266502ac6454SAndrew Thompson { 266602ac6454SAndrew Thompson return (iface->alt_index); 266702ac6454SAndrew Thompson } 266802ac6454SAndrew Thompson 266902ac6454SAndrew Thompson uint8_t 2670a593f6b8SAndrew Thompson usbd_get_bus_index(struct usb_device *udev) 267102ac6454SAndrew Thompson { 267202ac6454SAndrew Thompson return ((uint8_t)device_get_unit(udev->bus->bdev)); 267302ac6454SAndrew Thompson } 267402ac6454SAndrew Thompson 267502ac6454SAndrew Thompson uint8_t 2676a593f6b8SAndrew Thompson usbd_get_device_index(struct usb_device *udev) 267702ac6454SAndrew Thompson { 267802ac6454SAndrew Thompson return (udev->device_index); 267902ac6454SAndrew Thompson } 268002ac6454SAndrew Thompson 26818bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 26823acd904dSAndrew Thompson static void 26833acd904dSAndrew Thompson usb_notify_addq(const char *type, struct usb_device *udev) 26843acd904dSAndrew Thompson { 26853acd904dSAndrew Thompson struct usb_interface *iface; 26863acd904dSAndrew Thompson struct sbuf *sb; 26873acd904dSAndrew Thompson int i; 26883acd904dSAndrew Thompson 26893acd904dSAndrew Thompson /* announce the device */ 26903acd904dSAndrew Thompson sb = sbuf_new_auto(); 26913acd904dSAndrew Thompson sbuf_printf(sb, 26928bb77249SHans Petter Selasky #if USB_HAVE_UGEN 26938bb77249SHans Petter Selasky "ugen=%s " 269402c40241SHans Petter Selasky "cdev=%s " 26958bb77249SHans Petter Selasky #endif 26963acd904dSAndrew Thompson "vendor=0x%04x " 26973acd904dSAndrew Thompson "product=0x%04x " 26983acd904dSAndrew Thompson "devclass=0x%02x " 26993acd904dSAndrew Thompson "devsubclass=0x%02x " 27003acd904dSAndrew Thompson "sernum=\"%s\" " 27013acd904dSAndrew Thompson "release=0x%04x " 270266390216SAndrew Thompson "mode=%s " 27033acd904dSAndrew Thompson "port=%u " 27048bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27056df3eebbSNick Hibma "parent=%s" 27068bb77249SHans Petter Selasky #endif 27078bb77249SHans Petter Selasky "", 27088bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27093acd904dSAndrew Thompson udev->ugen_name, 271002c40241SHans Petter Selasky udev->ugen_name, 27118bb77249SHans Petter Selasky #endif 27123acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 27133acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 27143acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 27153acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2716ae538d85SAndrew Thompson usb_get_serial(udev), 27173acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 271866390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 27198bb77249SHans Petter Selasky udev->port_no 27208bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27218bb77249SHans Petter Selasky , udev->parent_hub != NULL ? 27223acd904dSAndrew Thompson udev->parent_hub->ugen_name : 27238bb77249SHans Petter Selasky device_get_nameunit(device_get_parent(udev->bus->bdev)) 27248bb77249SHans Petter Selasky #endif 27258bb77249SHans Petter Selasky ); 27263acd904dSAndrew Thompson sbuf_finish(sb); 27273acd904dSAndrew Thompson devctl_notify("USB", "DEVICE", type, sbuf_data(sb)); 27283acd904dSAndrew Thompson sbuf_delete(sb); 27293acd904dSAndrew Thompson 27303acd904dSAndrew Thompson /* announce each interface */ 27313acd904dSAndrew Thompson for (i = 0; i < USB_IFACE_MAX; i++) { 27323acd904dSAndrew Thompson iface = usbd_get_iface(udev, i); 27333acd904dSAndrew Thompson if (iface == NULL) 27343acd904dSAndrew Thompson break; /* end of interfaces */ 27353acd904dSAndrew Thompson if (iface->idesc == NULL) 27363acd904dSAndrew Thompson continue; /* no interface descriptor */ 27373acd904dSAndrew Thompson 27383acd904dSAndrew Thompson sb = sbuf_new_auto(); 27393acd904dSAndrew Thompson sbuf_printf(sb, 27408bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27418bb77249SHans Petter Selasky "ugen=%s " 274202c40241SHans Petter Selasky "cdev=%s " 27438bb77249SHans Petter Selasky #endif 27443acd904dSAndrew Thompson "vendor=0x%04x " 27453acd904dSAndrew Thompson "product=0x%04x " 27463acd904dSAndrew Thompson "devclass=0x%02x " 27473acd904dSAndrew Thompson "devsubclass=0x%02x " 27483acd904dSAndrew Thompson "sernum=\"%s\" " 27493acd904dSAndrew Thompson "release=0x%04x " 275066390216SAndrew Thompson "mode=%s " 27513acd904dSAndrew Thompson "interface=%d " 27523acd904dSAndrew Thompson "endpoints=%d " 27533acd904dSAndrew Thompson "intclass=0x%02x " 27543acd904dSAndrew Thompson "intsubclass=0x%02x " 27556df3eebbSNick Hibma "intprotocol=0x%02x", 27568bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27573acd904dSAndrew Thompson udev->ugen_name, 275802c40241SHans Petter Selasky udev->ugen_name, 27598bb77249SHans Petter Selasky #endif 27603acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 27613acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 27623acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 27633acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2764ae538d85SAndrew Thompson usb_get_serial(udev), 27653acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 276666390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 27673acd904dSAndrew Thompson iface->idesc->bInterfaceNumber, 27683acd904dSAndrew Thompson iface->idesc->bNumEndpoints, 27693acd904dSAndrew Thompson iface->idesc->bInterfaceClass, 27703acd904dSAndrew Thompson iface->idesc->bInterfaceSubClass, 27713acd904dSAndrew Thompson iface->idesc->bInterfaceProtocol); 27723acd904dSAndrew Thompson sbuf_finish(sb); 27733acd904dSAndrew Thompson devctl_notify("USB", "INTERFACE", type, sbuf_data(sb)); 27743acd904dSAndrew Thompson sbuf_delete(sb); 27753acd904dSAndrew Thompson } 27763acd904dSAndrew Thompson } 27778bb77249SHans Petter Selasky #endif 277802ac6454SAndrew Thompson 27798bb77249SHans Petter Selasky #if USB_HAVE_UGEN 278002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2781a593f6b8SAndrew Thompson * usb_fifo_free_wrap 278202ac6454SAndrew Thompson * 278302ac6454SAndrew Thompson * This function will free the FIFOs. 278402ac6454SAndrew Thompson * 2785bdd41206SAndrew Thompson * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2786bdd41206SAndrew Thompson * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2787bdd41206SAndrew Thompson * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2788bdd41206SAndrew Thompson * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2789bdd41206SAndrew Thompson * control endpoint FIFOs. If "iface_index" is not set to 2790bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" the flag has no effect. 279102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 279202ac6454SAndrew Thompson static void 2793a593f6b8SAndrew Thompson usb_fifo_free_wrap(struct usb_device *udev, 279402ac6454SAndrew Thompson uint8_t iface_index, uint8_t flag) 279502ac6454SAndrew Thompson { 2796760bc48eSAndrew Thompson struct usb_fifo *f; 279702ac6454SAndrew Thompson uint16_t i; 279802ac6454SAndrew Thompson 279902ac6454SAndrew Thompson /* 280002ac6454SAndrew Thompson * Free any USB FIFOs on the given interface: 280102ac6454SAndrew Thompson */ 280202ac6454SAndrew Thompson for (i = 0; i != USB_FIFO_MAX; i++) { 280302ac6454SAndrew Thompson f = udev->fifo[i]; 280402ac6454SAndrew Thompson if (f == NULL) { 280502ac6454SAndrew Thompson continue; 280602ac6454SAndrew Thompson } 280702ac6454SAndrew Thompson /* Check if the interface index matches */ 280802ac6454SAndrew Thompson if (iface_index == f->iface_index) { 2809a593f6b8SAndrew Thompson if (f->methods != &usb_ugen_methods) { 281002ac6454SAndrew Thompson /* 281102ac6454SAndrew Thompson * Don't free any non-generic FIFOs in 281202ac6454SAndrew Thompson * this case. 281302ac6454SAndrew Thompson */ 281402ac6454SAndrew Thompson continue; 281502ac6454SAndrew Thompson } 281602ac6454SAndrew Thompson if ((f->dev_ep_index == 0) && 281702ac6454SAndrew Thompson (f->fs_xfer == NULL)) { 281802ac6454SAndrew Thompson /* no need to free this FIFO */ 281902ac6454SAndrew Thompson continue; 282002ac6454SAndrew Thompson } 282102ac6454SAndrew Thompson } else if (iface_index == USB_IFACE_INDEX_ANY) { 2822a593f6b8SAndrew Thompson if ((f->methods == &usb_ugen_methods) && 2823bdd41206SAndrew Thompson (f->dev_ep_index == 0) && 2824bdd41206SAndrew Thompson (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 282502ac6454SAndrew Thompson (f->fs_xfer == NULL)) { 282602ac6454SAndrew Thompson /* no need to free this FIFO */ 282702ac6454SAndrew Thompson continue; 282802ac6454SAndrew Thompson } 282902ac6454SAndrew Thompson } else { 283002ac6454SAndrew Thompson /* no need to free this FIFO */ 283102ac6454SAndrew Thompson continue; 283202ac6454SAndrew Thompson } 283302ac6454SAndrew Thompson /* free this FIFO */ 2834a593f6b8SAndrew Thompson usb_fifo_free(f); 283502ac6454SAndrew Thompson } 283602ac6454SAndrew Thompson } 28370f6e8f93SAndrew Thompson #endif 283802ac6454SAndrew Thompson 283902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2840a593f6b8SAndrew Thompson * usb_peer_can_wakeup 284102ac6454SAndrew Thompson * 284202ac6454SAndrew Thompson * Return values: 284302ac6454SAndrew Thompson * 0: Peer cannot do resume signalling. 284402ac6454SAndrew Thompson * Else: Peer can do resume signalling. 284502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 284602ac6454SAndrew Thompson uint8_t 2847a593f6b8SAndrew Thompson usb_peer_can_wakeup(struct usb_device *udev) 284802ac6454SAndrew Thompson { 2849760bc48eSAndrew Thompson const struct usb_config_descriptor *cdp; 285002ac6454SAndrew Thompson 285102ac6454SAndrew Thompson cdp = udev->cdesc; 2852f29a0724SAndrew Thompson if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) { 285302ac6454SAndrew Thompson return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 285402ac6454SAndrew Thompson } 285502ac6454SAndrew Thompson return (0); /* not supported */ 285602ac6454SAndrew Thompson } 2857bd216778SAndrew Thompson 2858bd216778SAndrew Thompson void 2859a593f6b8SAndrew Thompson usb_set_device_state(struct usb_device *udev, enum usb_dev_state state) 2860bd216778SAndrew Thompson { 2861bd216778SAndrew Thompson 2862bd216778SAndrew Thompson KASSERT(state < USB_STATE_MAX, ("invalid udev state")); 2863bd216778SAndrew Thompson 2864bd216778SAndrew Thompson DPRINTF("udev %p state %s -> %s\n", udev, 2865a593f6b8SAndrew Thompson usb_statestr(udev->state), usb_statestr(state)); 2866963169b4SHans Petter Selasky 2867f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2868f97da128SHans Petter Selasky mtx_lock(&usb_ref_lock); 2869f97da128SHans Petter Selasky #endif 2870f97da128SHans Petter Selasky udev->state = state; 2871f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2872f97da128SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2873f97da128SHans Petter Selasky #endif 2874963169b4SHans Petter Selasky if (udev->bus->methods->device_state_change != NULL) 2875963169b4SHans Petter Selasky (udev->bus->methods->device_state_change) (udev); 2876963169b4SHans Petter Selasky } 2877963169b4SHans Petter Selasky 2878963169b4SHans Petter Selasky enum usb_dev_state 2879963169b4SHans Petter Selasky usb_get_device_state(struct usb_device *udev) 2880963169b4SHans Petter Selasky { 2881963169b4SHans Petter Selasky if (udev == NULL) 2882963169b4SHans Petter Selasky return (USB_STATE_DETACHED); 2883963169b4SHans Petter Selasky return (udev->state); 2884bd216778SAndrew Thompson } 2885bd216778SAndrew Thompson 2886ec8f3127SAndrew Thompson uint8_t 2887a593f6b8SAndrew Thompson usbd_device_attached(struct usb_device *udev) 2888bd216778SAndrew Thompson { 2889bd216778SAndrew Thompson return (udev->state > USB_STATE_DETACHED); 2890bd216778SAndrew Thompson } 2891cb18f7d1SAlfred Perlstein 28926950c75fSHans Petter Selasky /* 28936950c75fSHans Petter Selasky * The following function locks enumerating the given USB device. If 28946950c75fSHans Petter Selasky * the lock is already grabbed this function returns zero. Else a 289510aab8b6SHans Petter Selasky * a value of one is returned. 28966950c75fSHans Petter Selasky */ 28976950c75fSHans Petter Selasky uint8_t 2898cb18f7d1SAlfred Perlstein usbd_enum_lock(struct usb_device *udev) 2899cb18f7d1SAlfred Perlstein { 29006950c75fSHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 29016950c75fSHans Petter Selasky return (0); 29026950c75fSHans Petter Selasky 290391cd9240SAndrew Thompson sx_xlock(&udev->enum_sx); 29042df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 2905cb18f7d1SAlfred Perlstein /* 2906cb18f7d1SAlfred Perlstein * NEWBUS LOCK NOTE: We should check if any parent SX locks 2907cb18f7d1SAlfred Perlstein * are locked before locking Giant. Else the lock can be 2908cb18f7d1SAlfred Perlstein * locked multiple times. 2909cb18f7d1SAlfred Perlstein */ 2910c6df6f53SWarner Losh bus_topo_lock(); 29116950c75fSHans Petter Selasky return (1); 2912cb18f7d1SAlfred Perlstein } 2913cb18f7d1SAlfred Perlstein 291410aab8b6SHans Petter Selasky #if USB_HAVE_UGEN 291510aab8b6SHans Petter Selasky /* 291610aab8b6SHans Petter Selasky * This function is the same like usbd_enum_lock() except a value of 291710aab8b6SHans Petter Selasky * 255 is returned when a signal is pending: 291810aab8b6SHans Petter Selasky */ 291910aab8b6SHans Petter Selasky uint8_t 292010aab8b6SHans Petter Selasky usbd_enum_lock_sig(struct usb_device *udev) 292110aab8b6SHans Petter Selasky { 292210aab8b6SHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 292310aab8b6SHans Petter Selasky return (0); 292410aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->enum_sx)) 292510aab8b6SHans Petter Selasky return (255); 292610aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->sr_sx)) { 292710aab8b6SHans Petter Selasky sx_xunlock(&udev->enum_sx); 292810aab8b6SHans Petter Selasky return (255); 292910aab8b6SHans Petter Selasky } 2930c6df6f53SWarner Losh bus_topo_lock(); 293110aab8b6SHans Petter Selasky return (1); 293210aab8b6SHans Petter Selasky } 293310aab8b6SHans Petter Selasky #endif 293410aab8b6SHans Petter Selasky 2935cb18f7d1SAlfred Perlstein /* The following function unlocks enumerating the given USB device. */ 2936cb18f7d1SAlfred Perlstein 2937cb18f7d1SAlfred Perlstein void 2938cb18f7d1SAlfred Perlstein usbd_enum_unlock(struct usb_device *udev) 2939cb18f7d1SAlfred Perlstein { 2940c6df6f53SWarner Losh bus_topo_unlock(); 294191cd9240SAndrew Thompson sx_xunlock(&udev->enum_sx); 29422df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 29432df1e9a6SAndrew Thompson } 29442df1e9a6SAndrew Thompson 29452df1e9a6SAndrew Thompson /* The following function locks suspend and resume. */ 29462df1e9a6SAndrew Thompson 29472df1e9a6SAndrew Thompson void 29482df1e9a6SAndrew Thompson usbd_sr_lock(struct usb_device *udev) 29492df1e9a6SAndrew Thompson { 29502df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 29512df1e9a6SAndrew Thompson /* 29522df1e9a6SAndrew Thompson * NEWBUS LOCK NOTE: We should check if any parent SX locks 29532df1e9a6SAndrew Thompson * are locked before locking Giant. Else the lock can be 29542df1e9a6SAndrew Thompson * locked multiple times. 29552df1e9a6SAndrew Thompson */ 2956c6df6f53SWarner Losh bus_topo_lock(); 29572df1e9a6SAndrew Thompson } 29582df1e9a6SAndrew Thompson 29592df1e9a6SAndrew Thompson /* The following function unlocks suspend and resume. */ 29602df1e9a6SAndrew Thompson 29612df1e9a6SAndrew Thompson void 29622df1e9a6SAndrew Thompson usbd_sr_unlock(struct usb_device *udev) 29632df1e9a6SAndrew Thompson { 2964c6df6f53SWarner Losh bus_topo_unlock(); 29652df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 2966cb18f7d1SAlfred Perlstein } 2967cb18f7d1SAlfred Perlstein 2968cb18f7d1SAlfred Perlstein /* 2969cb18f7d1SAlfred Perlstein * The following function checks the enumerating lock for the given 2970cb18f7d1SAlfred Perlstein * USB device. 2971cb18f7d1SAlfred Perlstein */ 2972cb18f7d1SAlfred Perlstein 2973cb18f7d1SAlfred Perlstein uint8_t 2974cb18f7d1SAlfred Perlstein usbd_enum_is_locked(struct usb_device *udev) 2975cb18f7d1SAlfred Perlstein { 297691cd9240SAndrew Thompson return (sx_xlocked(&udev->enum_sx)); 2977cb18f7d1SAlfred Perlstein } 29788427ed84SHans Petter Selasky 29798427ed84SHans Petter Selasky /* 298064cb5e2aSHans Petter Selasky * The following function is used to serialize access to USB control 298164cb5e2aSHans Petter Selasky * transfers and the USB scratch area. If the lock is already grabbed 298264cb5e2aSHans Petter Selasky * this function returns zero. Else a value of one is returned. 298364cb5e2aSHans Petter Selasky */ 298464cb5e2aSHans Petter Selasky uint8_t 298564cb5e2aSHans Petter Selasky usbd_ctrl_lock(struct usb_device *udev) 298664cb5e2aSHans Petter Selasky { 298764cb5e2aSHans Petter Selasky if (sx_xlocked(&udev->ctrl_sx)) 298864cb5e2aSHans Petter Selasky return (0); 298964cb5e2aSHans Petter Selasky sx_xlock(&udev->ctrl_sx); 299064cb5e2aSHans Petter Selasky 299164cb5e2aSHans Petter Selasky /* 299264cb5e2aSHans Petter Selasky * We need to allow suspend and resume at this point, else the 299364cb5e2aSHans Petter Selasky * control transfer will timeout if the device is suspended! 299464cb5e2aSHans Petter Selasky */ 299564cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 299664cb5e2aSHans Petter Selasky usbd_sr_unlock(udev); 299764cb5e2aSHans Petter Selasky return (1); 299864cb5e2aSHans Petter Selasky } 299964cb5e2aSHans Petter Selasky 300064cb5e2aSHans Petter Selasky void 300164cb5e2aSHans Petter Selasky usbd_ctrl_unlock(struct usb_device *udev) 300264cb5e2aSHans Petter Selasky { 300364cb5e2aSHans Petter Selasky sx_xunlock(&udev->ctrl_sx); 300464cb5e2aSHans Petter Selasky 300564cb5e2aSHans Petter Selasky /* 300664cb5e2aSHans Petter Selasky * Restore the suspend and resume lock after we have unlocked 300764cb5e2aSHans Petter Selasky * the USB control transfer lock to avoid LOR: 300864cb5e2aSHans Petter Selasky */ 300964cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 301064cb5e2aSHans Petter Selasky usbd_sr_lock(udev); 301164cb5e2aSHans Petter Selasky } 301264cb5e2aSHans Petter Selasky 301364cb5e2aSHans Petter Selasky /* 30148427ed84SHans Petter Selasky * The following function is used to set the per-interface specific 30158427ed84SHans Petter Selasky * plug and play information. The string referred to by the pnpinfo 30168427ed84SHans Petter Selasky * argument can safely be freed after calling this function. The 30178427ed84SHans Petter Selasky * pnpinfo of an interface will be reset at device detach or when 30188427ed84SHans Petter Selasky * passing a NULL argument to this function. This function 30198427ed84SHans Petter Selasky * returns zero on success, else a USB_ERR_XXX failure code. 30208427ed84SHans Petter Selasky */ 30218427ed84SHans Petter Selasky 30228427ed84SHans Petter Selasky usb_error_t 30238427ed84SHans Petter Selasky usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo) 30248427ed84SHans Petter Selasky { 30258427ed84SHans Petter Selasky struct usb_interface *iface; 30268427ed84SHans Petter Selasky 30278427ed84SHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 30288427ed84SHans Petter Selasky if (iface == NULL) 30298427ed84SHans Petter Selasky return (USB_ERR_INVAL); 30308427ed84SHans Petter Selasky 30318427ed84SHans Petter Selasky if (iface->pnpinfo != NULL) { 30328427ed84SHans Petter Selasky free(iface->pnpinfo, M_USBDEV); 30338427ed84SHans Petter Selasky iface->pnpinfo = NULL; 30348427ed84SHans Petter Selasky } 30358427ed84SHans Petter Selasky 30368427ed84SHans Petter Selasky if (pnpinfo == NULL || pnpinfo[0] == 0) 30378427ed84SHans Petter Selasky return (0); /* success */ 30388427ed84SHans Petter Selasky 30398427ed84SHans Petter Selasky iface->pnpinfo = strdup(pnpinfo, M_USBDEV); 30408427ed84SHans Petter Selasky if (iface->pnpinfo == NULL) 30418427ed84SHans Petter Selasky return (USB_ERR_NOMEM); 30428427ed84SHans Petter Selasky 30438427ed84SHans Petter Selasky return (0); /* success */ 30448427ed84SHans Petter Selasky } 30458427ed84SHans Petter Selasky 3046d46dc4adSHans Petter Selasky usb_error_t 3047d46dc4adSHans Petter Selasky usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk) 3048d46dc4adSHans Petter Selasky { 3049d46dc4adSHans Petter Selasky uint8_t x; 3050d46dc4adSHans Petter Selasky 3051d46dc4adSHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 30523f37fb62SHans Petter Selasky if (udev->autoQuirk[x] == 0 || 30533f37fb62SHans Petter Selasky udev->autoQuirk[x] == quirk) { 3054d46dc4adSHans Petter Selasky udev->autoQuirk[x] = quirk; 3055d46dc4adSHans Petter Selasky return (0); /* success */ 3056d46dc4adSHans Petter Selasky } 3057d46dc4adSHans Petter Selasky } 3058d46dc4adSHans Petter Selasky return (USB_ERR_NOMEM); 3059d46dc4adSHans Petter Selasky } 3060a5cf1aaaSHans Petter Selasky 3061a5cf1aaaSHans Petter Selasky /* 3062a5cf1aaaSHans Petter Selasky * The following function is used to select the endpoint mode. It 3063a5cf1aaaSHans Petter Selasky * should not be called outside enumeration context. 3064a5cf1aaaSHans Petter Selasky */ 3065a5cf1aaaSHans Petter Selasky 3066a5cf1aaaSHans Petter Selasky usb_error_t 3067a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep, 3068a5cf1aaaSHans Petter Selasky uint8_t ep_mode) 3069a5cf1aaaSHans Petter Selasky { 3070a5cf1aaaSHans Petter Selasky usb_error_t error; 3071f61fc4bdSHans Petter Selasky uint8_t do_unlock; 3072a5cf1aaaSHans Petter Selasky 30736950c75fSHans Petter Selasky /* Prevent re-enumeration */ 30746950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 3075a5cf1aaaSHans Petter Selasky 3076a5cf1aaaSHans Petter Selasky if (udev->bus->methods->set_endpoint_mode != NULL) { 3077a5cf1aaaSHans Petter Selasky error = (udev->bus->methods->set_endpoint_mode) ( 3078a5cf1aaaSHans Petter Selasky udev, ep, ep_mode); 3079a5cf1aaaSHans Petter Selasky } else if (ep_mode != USB_EP_MODE_DEFAULT) { 3080a5cf1aaaSHans Petter Selasky error = USB_ERR_INVAL; 3081a5cf1aaaSHans Petter Selasky } else { 3082a5cf1aaaSHans Petter Selasky error = 0; 3083a5cf1aaaSHans Petter Selasky } 3084a5cf1aaaSHans Petter Selasky 3085a5cf1aaaSHans Petter Selasky /* only set new mode regardless of error */ 3086a5cf1aaaSHans Petter Selasky ep->ep_mode = ep_mode; 3087a5cf1aaaSHans Petter Selasky 3088f61fc4bdSHans Petter Selasky if (do_unlock) 3089f61fc4bdSHans Petter Selasky usbd_enum_unlock(udev); 3090a5cf1aaaSHans Petter Selasky return (error); 3091a5cf1aaaSHans Petter Selasky } 3092a5cf1aaaSHans Petter Selasky 3093a5cf1aaaSHans Petter Selasky uint8_t 3094a5cf1aaaSHans Petter Selasky usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep) 3095a5cf1aaaSHans Petter Selasky { 3096a5cf1aaaSHans Petter Selasky return (ep->ep_mode); 3097a5cf1aaaSHans Petter Selasky } 3098