102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 3718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4718cf2ccSPedro F. Giffuni * 502ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 602ac6454SAndrew Thompson * 702ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 802ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 902ac6454SAndrew Thompson * are met: 1002ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1202ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1302ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1402ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1502ac6454SAndrew Thompson * 1602ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1702ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1802ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1902ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2002ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2102ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2202ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2302ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2402ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2502ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2602ac6454SAndrew Thompson * SUCH DAMAGE. 2702ac6454SAndrew Thompson */ 2802ac6454SAndrew Thompson 29d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 30d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 31d2b99310SHans Petter Selasky #else 32ed6d949aSAndrew Thompson #include <sys/stdint.h> 33ed6d949aSAndrew Thompson #include <sys/stddef.h> 34ed6d949aSAndrew Thompson #include <sys/param.h> 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++) { 32802ac6454SAndrew Thompson 329ae60fdfbSAndrew Thompson if (ep->edesc == NULL) { 33002ac6454SAndrew Thompson continue; 33102ac6454SAndrew Thompson } 33202ac6454SAndrew Thompson /* do the mask and check the value */ 333ae60fdfbSAndrew Thompson if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) { 33402ac6454SAndrew Thompson goto found; 33502ac6454SAndrew Thompson } 33602ac6454SAndrew Thompson } 33702ac6454SAndrew Thompson 33802ac6454SAndrew Thompson /* 339ae60fdfbSAndrew Thompson * The default endpoint is always present and is checked separately: 34002ac6454SAndrew Thompson */ 34122039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 3425b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 3435b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 34402ac6454SAndrew Thompson goto found; 34502ac6454SAndrew Thompson } 34602ac6454SAndrew Thompson return (NULL); 34702ac6454SAndrew Thompson 34802ac6454SAndrew Thompson found: 349ae60fdfbSAndrew Thompson return (ep); 35002ac6454SAndrew Thompson } 35102ac6454SAndrew Thompson 35202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 353a593f6b8SAndrew Thompson * usbd_get_endpoint 35402ac6454SAndrew Thompson * 355ae60fdfbSAndrew Thompson * This function searches for an USB endpoint based on the information 356760bc48eSAndrew Thompson * given by the passed "struct usb_config" pointer. 35702ac6454SAndrew Thompson * 35802ac6454SAndrew Thompson * Return values: 35902ac6454SAndrew Thompson * NULL: No match. 360ae60fdfbSAndrew Thompson * Else: Pointer to "struct usb_endpoint". 36102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 362ae60fdfbSAndrew Thompson struct usb_endpoint * 363a593f6b8SAndrew Thompson usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index, 364760bc48eSAndrew Thompson const struct usb_config *setup) 36502ac6454SAndrew Thompson { 366ae60fdfbSAndrew Thompson struct usb_endpoint *ep = udev->endpoints; 367ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 36802ac6454SAndrew Thompson uint8_t index = setup->ep_index; 36902ac6454SAndrew Thompson uint8_t ea_mask; 37002ac6454SAndrew Thompson uint8_t ea_val; 37102ac6454SAndrew Thompson uint8_t type_mask; 37202ac6454SAndrew Thompson uint8_t type_val; 37302ac6454SAndrew Thompson 37402ac6454SAndrew Thompson DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 37502ac6454SAndrew Thompson "type=0x%x dir=0x%x index=%d\n", 37602ac6454SAndrew Thompson udev, iface_index, setup->endpoint, 37702ac6454SAndrew Thompson setup->type, setup->direction, setup->ep_index); 37802ac6454SAndrew Thompson 3794eae601eSAndrew Thompson /* check USB mode */ 3804eae601eSAndrew Thompson 381f29a0724SAndrew Thompson if (setup->usb_mode != USB_MODE_DUAL && 382f29a0724SAndrew Thompson udev->flags.usb_mode != setup->usb_mode) { 383ae60fdfbSAndrew Thompson /* wrong mode - no endpoint */ 3844eae601eSAndrew Thompson return (NULL); 3854eae601eSAndrew Thompson } 3864eae601eSAndrew Thompson 38702ac6454SAndrew Thompson /* setup expected endpoint direction mask and value */ 38802ac6454SAndrew Thompson 38951ec1603SAndrew Thompson if (setup->direction == UE_DIR_RX) { 39051ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 391f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 39251ec1603SAndrew Thompson UE_DIR_OUT : UE_DIR_IN; 39351ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_TX) { 39451ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 395f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 39651ec1603SAndrew Thompson UE_DIR_IN : UE_DIR_OUT; 39751ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_ANY) { 39802ac6454SAndrew Thompson /* match any endpoint direction */ 39902ac6454SAndrew Thompson ea_mask = 0; 40002ac6454SAndrew Thompson ea_val = 0; 40102ac6454SAndrew Thompson } else { 40202ac6454SAndrew Thompson /* match the given endpoint direction */ 40302ac6454SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 40402ac6454SAndrew Thompson ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 40502ac6454SAndrew Thompson } 40602ac6454SAndrew Thompson 40702ac6454SAndrew Thompson /* setup expected endpoint address */ 40802ac6454SAndrew Thompson 40902ac6454SAndrew Thompson if (setup->endpoint == UE_ADDR_ANY) { 41002ac6454SAndrew Thompson /* match any endpoint address */ 41102ac6454SAndrew Thompson } else { 41202ac6454SAndrew Thompson /* match the given endpoint address */ 41302ac6454SAndrew Thompson ea_mask |= UE_ADDR; 41402ac6454SAndrew Thompson ea_val |= (setup->endpoint & UE_ADDR); 41502ac6454SAndrew Thompson } 41602ac6454SAndrew Thompson 41702ac6454SAndrew Thompson /* setup expected endpoint type */ 41802ac6454SAndrew Thompson 41902ac6454SAndrew Thompson if (setup->type == UE_BULK_INTR) { 42002ac6454SAndrew Thompson /* this will match BULK and INTERRUPT endpoints */ 42102ac6454SAndrew Thompson type_mask = 2; 42202ac6454SAndrew Thompson type_val = 2; 42302ac6454SAndrew Thompson } else if (setup->type == UE_TYPE_ANY) { 42402ac6454SAndrew Thompson /* match any endpoint type */ 42502ac6454SAndrew Thompson type_mask = 0; 42602ac6454SAndrew Thompson type_val = 0; 42702ac6454SAndrew Thompson } else { 42802ac6454SAndrew Thompson /* match the given endpoint type */ 42902ac6454SAndrew Thompson type_mask = UE_XFERTYPE; 43002ac6454SAndrew Thompson type_val = (setup->type & UE_XFERTYPE); 43102ac6454SAndrew Thompson } 43202ac6454SAndrew Thompson 43302ac6454SAndrew Thompson /* 43420733245SPedro F. Giffuni * Iterate across all the USB endpoints searching for a match 43502ac6454SAndrew Thompson * based on the endpoint address. Note that we are searching 436ae60fdfbSAndrew Thompson * the endpoints from the beginning of the "udev->endpoints" array. 43702ac6454SAndrew Thompson */ 438ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 43902ac6454SAndrew Thompson 440ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 441ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 44202ac6454SAndrew Thompson continue; 44302ac6454SAndrew Thompson } 44402ac6454SAndrew Thompson /* do the masks and check the values */ 44502ac6454SAndrew Thompson 446ae60fdfbSAndrew Thompson if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) && 447ae60fdfbSAndrew Thompson ((ep->edesc->bmAttributes & type_mask) == type_val)) { 44802ac6454SAndrew Thompson if (!index--) { 44902ac6454SAndrew Thompson goto found; 45002ac6454SAndrew Thompson } 45102ac6454SAndrew Thompson } 45202ac6454SAndrew Thompson } 45302ac6454SAndrew Thompson 45402ac6454SAndrew Thompson /* 455ae60fdfbSAndrew Thompson * Match against default endpoint last, so that "any endpoint", "any 456ae60fdfbSAndrew Thompson * address" and "any direction" returns the first endpoint of the 45702ac6454SAndrew Thompson * interface. "iface_index" and "direction" is ignored: 45802ac6454SAndrew Thompson */ 45922039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 4605b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) && 4615b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) && 46202ac6454SAndrew Thompson (!index)) { 4635b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 46402ac6454SAndrew Thompson goto found; 46502ac6454SAndrew Thompson } 46602ac6454SAndrew Thompson return (NULL); 46702ac6454SAndrew Thompson 46802ac6454SAndrew Thompson found: 469ae60fdfbSAndrew Thompson return (ep); 47002ac6454SAndrew Thompson } 47102ac6454SAndrew Thompson 47202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 473ed6d949aSAndrew Thompson * usbd_interface_count 47402ac6454SAndrew Thompson * 47502ac6454SAndrew Thompson * This function stores the number of USB interfaces excluding 47602ac6454SAndrew Thompson * alternate settings, which the USB config descriptor reports into 47702ac6454SAndrew Thompson * the unsigned 8-bit integer pointed to by "count". 47802ac6454SAndrew Thompson * 47902ac6454SAndrew Thompson * Returns: 48002ac6454SAndrew Thompson * 0: Success 48102ac6454SAndrew Thompson * Else: Failure 48202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 483e0a69b51SAndrew Thompson usb_error_t 484ed6d949aSAndrew Thompson usbd_interface_count(struct usb_device *udev, uint8_t *count) 48502ac6454SAndrew Thompson { 48602ac6454SAndrew Thompson if (udev->cdesc == NULL) { 48702ac6454SAndrew Thompson *count = 0; 48802ac6454SAndrew Thompson return (USB_ERR_NOT_CONFIGURED); 48902ac6454SAndrew Thompson } 490bdd41206SAndrew Thompson *count = udev->ifaces_max; 49102ac6454SAndrew Thompson return (USB_ERR_NORMAL_COMPLETION); 49202ac6454SAndrew Thompson } 49302ac6454SAndrew Thompson 49402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 495a593f6b8SAndrew Thompson * usb_init_endpoint 49602ac6454SAndrew Thompson * 497ae60fdfbSAndrew Thompson * This function will initialise the USB endpoint structure pointed to by 498ae60fdfbSAndrew Thompson * the "endpoint" argument. The structure pointed to by "endpoint" must be 499bdd41206SAndrew Thompson * zeroed before calling this function. 50002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 50102ac6454SAndrew Thompson static void 502a593f6b8SAndrew Thompson usb_init_endpoint(struct usb_device *udev, uint8_t iface_index, 503963169b4SHans Petter Selasky struct usb_endpoint_descriptor *edesc, 504963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *ecomp, 505963169b4SHans Petter Selasky struct usb_endpoint *ep) 50602ac6454SAndrew Thompson { 507e892b3feSHans Petter Selasky const struct usb_bus_methods *methods; 508a5cf1aaaSHans Petter Selasky usb_stream_t x; 509ee3e3ff5SAndrew Thompson 510bdd41206SAndrew Thompson methods = udev->bus->methods; 51102ac6454SAndrew Thompson 512ae60fdfbSAndrew Thompson (methods->endpoint_init) (udev, edesc, ep); 51302ac6454SAndrew Thompson 514ae60fdfbSAndrew Thompson /* initialise USB endpoint structure */ 515ae60fdfbSAndrew Thompson ep->edesc = edesc; 516963169b4SHans Petter Selasky ep->ecomp = ecomp; 517ae60fdfbSAndrew Thompson ep->iface_index = iface_index; 518a5cf1aaaSHans Petter Selasky 519a5cf1aaaSHans Petter Selasky /* setup USB stream queues */ 520a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 521a5cf1aaaSHans Petter Selasky TAILQ_INIT(&ep->endpoint_q[x].head); 522a5cf1aaaSHans Petter Selasky ep->endpoint_q[x].command = &usbd_pipe_start; 523a5cf1aaaSHans Petter Selasky } 52402ac6454SAndrew Thompson 52539307315SAndrew Thompson /* the pipe is not supported by the hardware */ 526ae60fdfbSAndrew Thompson if (ep->methods == NULL) 52739307315SAndrew Thompson return; 52839307315SAndrew Thompson 529a5cf1aaaSHans Petter Selasky /* check for SUPER-speed streams mode endpoint */ 530a5cf1aaaSHans Petter Selasky if (udev->speed == USB_SPEED_SUPER && ecomp != NULL && 531a5cf1aaaSHans Petter Selasky (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK && 532a5cf1aaaSHans Petter Selasky (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) { 533a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS); 534a5cf1aaaSHans Petter Selasky } else { 535a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT); 536a5cf1aaaSHans Petter Selasky } 537a5cf1aaaSHans Petter Selasky 53802ac6454SAndrew Thompson /* clear stall, if any */ 539bdd41206SAndrew Thompson if (methods->clear_stall != NULL) { 54002ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 541ae60fdfbSAndrew Thompson (methods->clear_stall) (udev, ep); 54202ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 54302ac6454SAndrew Thompson } 54402ac6454SAndrew Thompson } 54502ac6454SAndrew Thompson 546bdd41206SAndrew Thompson /*-----------------------------------------------------------------------* 547a593f6b8SAndrew Thompson * usb_endpoint_foreach 54816589beaSAndrew Thompson * 54916589beaSAndrew Thompson * This function will iterate all the USB endpoints except the control 55016589beaSAndrew Thompson * endpoint. This function is NULL safe. 55116589beaSAndrew Thompson * 55216589beaSAndrew Thompson * Return values: 553ae60fdfbSAndrew Thompson * NULL: End of USB endpoints 554ae60fdfbSAndrew Thompson * Else: Pointer to next USB endpoint 55516589beaSAndrew Thompson *------------------------------------------------------------------------*/ 556ae60fdfbSAndrew Thompson struct usb_endpoint * 557a593f6b8SAndrew Thompson usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep) 55816589beaSAndrew Thompson { 559a9e26757SAndrew Thompson struct usb_endpoint *ep_end; 56016589beaSAndrew Thompson 56116589beaSAndrew Thompson /* be NULL safe */ 56216589beaSAndrew Thompson if (udev == NULL) 56316589beaSAndrew Thompson return (NULL); 56416589beaSAndrew Thompson 565a9e26757SAndrew Thompson ep_end = udev->endpoints + udev->endpoints_max; 566a9e26757SAndrew Thompson 567ae60fdfbSAndrew Thompson /* get next endpoint */ 568ae60fdfbSAndrew Thompson if (ep == NULL) 569ae60fdfbSAndrew Thompson ep = udev->endpoints; 57016589beaSAndrew Thompson else 571ae60fdfbSAndrew Thompson ep++; 57216589beaSAndrew Thompson 573ae60fdfbSAndrew Thompson /* find next allocated ep */ 574ae60fdfbSAndrew Thompson while (ep != ep_end) { 575ae60fdfbSAndrew Thompson if (ep->edesc != NULL) 576ae60fdfbSAndrew Thompson return (ep); 577ae60fdfbSAndrew Thompson ep++; 57816589beaSAndrew Thompson } 57916589beaSAndrew Thompson return (NULL); 58016589beaSAndrew Thompson } 58116589beaSAndrew Thompson 58216589beaSAndrew Thompson /*------------------------------------------------------------------------* 583b78e84d1SHans Petter Selasky * usb_wait_pending_refs 584d008478eSHans Petter Selasky * 585d008478eSHans Petter Selasky * This function will wait for any USB references to go away before 586b78e84d1SHans Petter Selasky * returning. This function is used before freeing a USB device. 587d008478eSHans Petter Selasky *------------------------------------------------------------------------*/ 588d008478eSHans Petter Selasky static void 589b78e84d1SHans Petter Selasky usb_wait_pending_refs(struct usb_device *udev) 590d008478eSHans Petter Selasky { 591d008478eSHans Petter Selasky #if USB_HAVE_UGEN 592b78e84d1SHans Petter Selasky DPRINTF("Refcount = %d\n", (int)udev->refcount); 593d008478eSHans Petter Selasky 594b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 595b78e84d1SHans Petter Selasky udev->refcount--; 596d008478eSHans Petter Selasky while (1) { 597d008478eSHans Petter Selasky /* wait for any pending references to go away */ 598b78e84d1SHans Petter Selasky if (udev->refcount == 0) { 599b78e84d1SHans Petter Selasky /* prevent further refs being taken, if any */ 600d008478eSHans Petter Selasky udev->refcount = USB_DEV_REF_MAX; 601d008478eSHans Petter Selasky break; 602d008478eSHans Petter Selasky } 603d008478eSHans Petter Selasky cv_wait(&udev->ref_cv, &usb_ref_lock); 604d008478eSHans Petter Selasky } 605d008478eSHans Petter Selasky mtx_unlock(&usb_ref_lock); 606d008478eSHans Petter Selasky #endif 607d008478eSHans Petter Selasky } 608d008478eSHans Petter Selasky 609d008478eSHans Petter Selasky /*------------------------------------------------------------------------* 610a593f6b8SAndrew Thompson * usb_unconfigure 61102ac6454SAndrew Thompson * 612ae60fdfbSAndrew Thompson * This function will free all USB interfaces and USB endpoints belonging 61302ac6454SAndrew Thompson * to an USB device. 614bdd41206SAndrew Thompson * 615bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 61602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 61702ac6454SAndrew Thompson static void 618a593f6b8SAndrew Thompson usb_unconfigure(struct usb_device *udev, uint8_t flag) 61902ac6454SAndrew Thompson { 620bdd41206SAndrew Thompson uint8_t do_unlock; 62102ac6454SAndrew Thompson 6226950c75fSHans Petter Selasky /* Prevent re-enumeration */ 6236950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 624bdd41206SAndrew Thompson 625bdd41206SAndrew Thompson /* detach all interface drivers */ 626a593f6b8SAndrew Thompson usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag); 627bdd41206SAndrew Thompson 628bdd41206SAndrew Thompson #if USB_HAVE_UGEN 629bdd41206SAndrew Thompson /* free all FIFOs except control endpoint FIFOs */ 630a593f6b8SAndrew Thompson usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag); 631bdd41206SAndrew Thompson 632bdd41206SAndrew Thompson /* 633bdd41206SAndrew Thompson * Free all cdev's, if any. 634bdd41206SAndrew Thompson */ 635a593f6b8SAndrew Thompson usb_cdev_free(udev); 636bdd41206SAndrew Thompson #endif 63702ac6454SAndrew Thompson 638bdc081c6SAndrew Thompson #if USB_HAVE_COMPAT_LINUX 63902ac6454SAndrew Thompson /* free Linux compat device, if any */ 6408d59ecb2SHans Petter Selasky if (udev->linux_endpoint_start != NULL) { 6418d59ecb2SHans Petter Selasky usb_linux_free_device_p(udev); 642760bc48eSAndrew Thompson udev->linux_endpoint_start = NULL; 64302ac6454SAndrew Thompson } 644bdc081c6SAndrew Thompson #endif 64502ac6454SAndrew Thompson 646a593f6b8SAndrew Thompson usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE); 64702ac6454SAndrew Thompson 648ae60fdfbSAndrew Thompson /* free "cdesc" after "ifaces" and "endpoints", if any */ 6497efaaa9aSAndrew Thompson if (udev->cdesc != NULL) { 650f29a0724SAndrew Thompson if (udev->flags.usb_mode != USB_MODE_DEVICE) 6512c79a775SHans Petter Selasky usbd_free_config_desc(udev, udev->cdesc); 65202ac6454SAndrew Thompson udev->cdesc = NULL; 65302ac6454SAndrew Thompson } 65402ac6454SAndrew Thompson /* set unconfigured state */ 65502ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 65602ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 657bdd41206SAndrew Thompson 658cb18f7d1SAlfred Perlstein if (do_unlock) 659cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 66002ac6454SAndrew Thompson } 66102ac6454SAndrew Thompson 66202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 663a593f6b8SAndrew Thompson * usbd_set_config_index 66402ac6454SAndrew Thompson * 66502ac6454SAndrew Thompson * This function selects configuration by index, independent of the 66602ac6454SAndrew Thompson * actual configuration number. This function should not be used by 66702ac6454SAndrew Thompson * USB drivers. 66802ac6454SAndrew Thompson * 66902ac6454SAndrew Thompson * Returns: 67002ac6454SAndrew Thompson * 0: Success 67102ac6454SAndrew Thompson * Else: Failure 67202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 673e0a69b51SAndrew Thompson usb_error_t 674a593f6b8SAndrew Thompson usbd_set_config_index(struct usb_device *udev, uint8_t index) 67502ac6454SAndrew Thompson { 676760bc48eSAndrew Thompson struct usb_status ds; 677760bc48eSAndrew Thompson struct usb_config_descriptor *cdp; 67802ac6454SAndrew Thompson uint16_t power; 67902ac6454SAndrew Thompson uint16_t max_power; 68002ac6454SAndrew Thompson uint8_t selfpowered; 68102ac6454SAndrew Thompson uint8_t do_unlock; 682e0a69b51SAndrew Thompson usb_error_t err; 68302ac6454SAndrew Thompson 68402ac6454SAndrew Thompson DPRINTFN(6, "udev=%p index=%d\n", udev, index); 68502ac6454SAndrew Thompson 6866950c75fSHans Petter Selasky /* Prevent re-enumeration */ 6876950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 68802ac6454SAndrew Thompson 689d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 69002ac6454SAndrew Thompson 69102ac6454SAndrew Thompson if (index == USB_UNCONFIG_INDEX) { 69202ac6454SAndrew Thompson /* 69302ac6454SAndrew Thompson * Leave unallocated when unconfiguring the 694a593f6b8SAndrew Thompson * device. "usb_unconfigure()" will also reset 69502ac6454SAndrew Thompson * the current config number and index. 69602ac6454SAndrew Thompson */ 697a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO); 698bd216778SAndrew Thompson if (udev->state == USB_STATE_CONFIGURED) 699a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 70002ac6454SAndrew Thompson goto done; 70102ac6454SAndrew Thompson } 70202ac6454SAndrew Thompson /* get the full config descriptor */ 703f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 7047efaaa9aSAndrew Thompson /* save some memory */ 705a593f6b8SAndrew Thompson err = usbd_req_get_descriptor_ptr(udev, &cdp, 706459d369eSAndrew Thompson (UDESC_CONFIG << 8) | index); 7077efaaa9aSAndrew Thompson } else { 7087efaaa9aSAndrew Thompson /* normal request */ 709a593f6b8SAndrew Thompson err = usbd_req_get_config_desc_full(udev, 7102c79a775SHans Petter Selasky NULL, &cdp, index); 7117efaaa9aSAndrew Thompson } 71202ac6454SAndrew Thompson if (err) { 71302ac6454SAndrew Thompson goto done; 71402ac6454SAndrew Thompson } 71502ac6454SAndrew Thompson /* set the new config descriptor */ 71602ac6454SAndrew Thompson 71702ac6454SAndrew Thompson udev->cdesc = cdp; 71802ac6454SAndrew Thompson 71902ac6454SAndrew Thompson /* Figure out if the device is self or bus powered. */ 72002ac6454SAndrew Thompson selfpowered = 0; 72102ac6454SAndrew Thompson if ((!udev->flags.uq_bus_powered) && 72202ac6454SAndrew Thompson (cdp->bmAttributes & UC_SELF_POWERED) && 723f29a0724SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST)) { 72402ac6454SAndrew Thompson /* May be self powered. */ 72502ac6454SAndrew Thompson if (cdp->bmAttributes & UC_BUS_POWERED) { 72602ac6454SAndrew Thompson /* Must ask device. */ 727a593f6b8SAndrew Thompson err = usbd_req_get_device_status(udev, NULL, &ds); 72802ac6454SAndrew Thompson if (err) { 72902ac6454SAndrew Thompson DPRINTFN(0, "could not read " 73002ac6454SAndrew Thompson "device status: %s\n", 731a593f6b8SAndrew Thompson usbd_errstr(err)); 73202ac6454SAndrew Thompson } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 73302ac6454SAndrew Thompson selfpowered = 1; 73402ac6454SAndrew Thompson } 73502ac6454SAndrew Thompson DPRINTF("status=0x%04x \n", 73602ac6454SAndrew Thompson UGETW(ds.wStatus)); 73702ac6454SAndrew Thompson } else 73802ac6454SAndrew Thompson selfpowered = 1; 73902ac6454SAndrew Thompson } 74002ac6454SAndrew Thompson DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 74102ac6454SAndrew Thompson "selfpowered=%d, power=%d\n", 74202ac6454SAndrew Thompson udev, cdp, 74391f72cedSAndrew Thompson udev->address, cdp->bConfigurationValue, cdp->bmAttributes, 74402ac6454SAndrew Thompson selfpowered, cdp->bMaxPower * 2); 74502ac6454SAndrew Thompson 74602ac6454SAndrew Thompson /* Check if we have enough power. */ 74702ac6454SAndrew Thompson power = cdp->bMaxPower * 2; 74802ac6454SAndrew Thompson 74902ac6454SAndrew Thompson if (udev->parent_hub) { 75002ac6454SAndrew Thompson max_power = udev->parent_hub->hub->portpower; 75102ac6454SAndrew Thompson } else { 75202ac6454SAndrew Thompson max_power = USB_MAX_POWER; 75302ac6454SAndrew Thompson } 75402ac6454SAndrew Thompson 75502ac6454SAndrew Thompson if (power > max_power) { 75602ac6454SAndrew Thompson DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 75702ac6454SAndrew Thompson err = USB_ERR_NO_POWER; 75802ac6454SAndrew Thompson goto done; 75902ac6454SAndrew Thompson } 76002ac6454SAndrew Thompson /* Only update "self_powered" in USB Host Mode */ 761f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 76202ac6454SAndrew Thompson udev->flags.self_powered = selfpowered; 76302ac6454SAndrew Thompson } 76402ac6454SAndrew Thompson udev->power = power; 76502ac6454SAndrew Thompson udev->curr_config_no = cdp->bConfigurationValue; 76602ac6454SAndrew Thompson udev->curr_config_index = index; 767a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_CONFIGURED); 76802ac6454SAndrew Thompson 76902ac6454SAndrew Thompson /* Set the actual configuration value. */ 770a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue); 77102ac6454SAndrew Thompson if (err) { 77202ac6454SAndrew Thompson goto done; 77302ac6454SAndrew Thompson } 774bdd41206SAndrew Thompson 775a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC); 77602ac6454SAndrew Thompson if (err) { 77702ac6454SAndrew Thompson goto done; 77802ac6454SAndrew Thompson } 779bdd41206SAndrew Thompson 780a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT); 781bdd41206SAndrew Thompson if (err) { 782bdd41206SAndrew Thompson goto done; 78302ac6454SAndrew Thompson } 784bdd41206SAndrew Thompson 7850f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 786ee3e3ff5SAndrew Thompson /* create device nodes for each endpoint */ 787a593f6b8SAndrew Thompson usb_cdev_create(udev); 7880f6e8f93SAndrew Thompson #endif 78902ac6454SAndrew Thompson 79002ac6454SAndrew Thompson done: 791a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err)); 79202ac6454SAndrew Thompson if (err) { 793d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 79402ac6454SAndrew Thompson } 795cb18f7d1SAlfred Perlstein if (do_unlock) 796cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 79702ac6454SAndrew Thompson return (err); 79802ac6454SAndrew Thompson } 79902ac6454SAndrew Thompson 80002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 801a593f6b8SAndrew Thompson * usb_config_parse 802bdd41206SAndrew Thompson * 803ae60fdfbSAndrew Thompson * This function will allocate and free USB interfaces and USB endpoints, 804ae60fdfbSAndrew Thompson * parse the USB configuration structure and initialise the USB endpoints 805bdd41206SAndrew Thompson * and interfaces. If "iface_index" is not equal to 806bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the 807bdd41206SAndrew Thompson * alternate_setting to be selected for the given interface. Else the 808bdd41206SAndrew Thompson * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be 809bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function 810bdd41206SAndrew Thompson * is typically called when setting the configuration or when setting 811bdd41206SAndrew Thompson * an alternate interface. 812bdd41206SAndrew Thompson * 813bdd41206SAndrew Thompson * Returns: 814bdd41206SAndrew Thompson * 0: Success 815bdd41206SAndrew Thompson * Else: Failure 816bdd41206SAndrew Thompson *------------------------------------------------------------------------*/ 817963169b4SHans Petter Selasky static usb_error_t 818a593f6b8SAndrew Thompson usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd) 819bdd41206SAndrew Thompson { 820760bc48eSAndrew Thompson struct usb_idesc_parse_state ips; 821760bc48eSAndrew Thompson struct usb_interface_descriptor *id; 822760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 823760bc48eSAndrew Thompson struct usb_interface *iface; 824ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 825e0a69b51SAndrew Thompson usb_error_t err; 826bdd41206SAndrew Thompson uint8_t ep_curr; 827bdd41206SAndrew Thompson uint8_t ep_max; 828bdd41206SAndrew Thompson uint8_t temp; 829bdd41206SAndrew Thompson uint8_t do_init; 830bdd41206SAndrew Thompson uint8_t alt_index; 831bdd41206SAndrew Thompson 832bdd41206SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 833bdd41206SAndrew Thompson /* parameter overload */ 834bdd41206SAndrew Thompson alt_index = cmd; 835bdd41206SAndrew Thompson cmd = USB_CFG_INIT; 836bdd41206SAndrew Thompson } else { 837bdd41206SAndrew Thompson /* not used */ 838bdd41206SAndrew Thompson alt_index = 0; 839bdd41206SAndrew Thompson } 840bdd41206SAndrew Thompson 841bdd41206SAndrew Thompson err = 0; 842bdd41206SAndrew Thompson 843bdd41206SAndrew Thompson DPRINTFN(5, "iface_index=%d cmd=%d\n", 844bdd41206SAndrew Thompson iface_index, cmd); 845bdd41206SAndrew Thompson 846bdd41206SAndrew Thompson if (cmd == USB_CFG_FREE) 847bdd41206SAndrew Thompson goto cleanup; 848bdd41206SAndrew Thompson 849bdd41206SAndrew Thompson if (cmd == USB_CFG_INIT) { 85091cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 851bdd41206SAndrew Thompson 852ae60fdfbSAndrew Thompson /* check for in-use endpoints */ 853bdd41206SAndrew Thompson 854ae60fdfbSAndrew Thompson ep = udev->endpoints; 855ae60fdfbSAndrew Thompson ep_max = udev->endpoints_max; 856bdd41206SAndrew Thompson while (ep_max--) { 857ae60fdfbSAndrew Thompson /* look for matching endpoints */ 858bdd41206SAndrew Thompson if ((iface_index == USB_IFACE_INDEX_ANY) || 859ae60fdfbSAndrew Thompson (iface_index == ep->iface_index)) { 860f12c6c29SAndrew Thompson if (ep->refcount_alloc != 0) { 861bdd41206SAndrew Thompson /* 862bdd41206SAndrew Thompson * This typically indicates a 863bdd41206SAndrew Thompson * more serious error. 864bdd41206SAndrew Thompson */ 865bdd41206SAndrew Thompson err = USB_ERR_IN_USE; 866bdd41206SAndrew Thompson } else { 867ae60fdfbSAndrew Thompson /* reset endpoint */ 868ae60fdfbSAndrew Thompson memset(ep, 0, sizeof(*ep)); 869ae60fdfbSAndrew Thompson /* make sure we don't zero the endpoint again */ 870ae60fdfbSAndrew Thompson ep->iface_index = USB_IFACE_INDEX_ANY; 871bdd41206SAndrew Thompson } 872bdd41206SAndrew Thompson } 873ae60fdfbSAndrew Thompson ep++; 874bdd41206SAndrew Thompson } 875bdd41206SAndrew Thompson 876bdd41206SAndrew Thompson if (err) 877bdd41206SAndrew Thompson return (err); 878bdd41206SAndrew Thompson } 879bdd41206SAndrew Thompson 880bdd41206SAndrew Thompson memset(&ips, 0, sizeof(ips)); 881bdd41206SAndrew Thompson 882bdd41206SAndrew Thompson ep_curr = 0; 883bdd41206SAndrew Thompson ep_max = 0; 884bdd41206SAndrew Thompson 885a593f6b8SAndrew Thompson while ((id = usb_idesc_foreach(udev->cdesc, &ips))) { 886bdd41206SAndrew Thompson 887bdd41206SAndrew Thompson iface = udev->ifaces + ips.iface_index; 888bdd41206SAndrew Thompson 889bdd41206SAndrew Thompson /* check for specific interface match */ 890bdd41206SAndrew Thompson 891bdd41206SAndrew Thompson if (cmd == USB_CFG_INIT) { 892bdd41206SAndrew Thompson if ((iface_index != USB_IFACE_INDEX_ANY) && 893bdd41206SAndrew Thompson (iface_index != ips.iface_index)) { 894bdd41206SAndrew Thompson /* wrong interface */ 895bdd41206SAndrew Thompson do_init = 0; 896bdd41206SAndrew Thompson } else if (alt_index != ips.iface_index_alt) { 897bdd41206SAndrew Thompson /* wrong alternate setting */ 898bdd41206SAndrew Thompson do_init = 0; 899bdd41206SAndrew Thompson } else { 900bdd41206SAndrew Thompson /* initialise interface */ 901bdd41206SAndrew Thompson do_init = 1; 902bdd41206SAndrew Thompson } 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 } 911bdd41206SAndrew Thompson /* check for init */ 912bdd41206SAndrew Thompson if (do_init) { 913bdd41206SAndrew Thompson /* setup the USB interface structure */ 914bdd41206SAndrew Thompson iface->idesc = id; 915bdd41206SAndrew Thompson /* set alternate index */ 916bdd41206SAndrew Thompson iface->alt_index = alt_index; 917d9073c1eSHans Petter Selasky /* set default interface parent */ 918d9073c1eSHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 919d9073c1eSHans Petter Selasky iface->parent_iface_index = 920d9073c1eSHans Petter Selasky USB_IFACE_INDEX_ANY; 921d9073c1eSHans Petter Selasky } 922bdd41206SAndrew Thompson } 923bdd41206SAndrew Thompson 924bdd41206SAndrew Thompson DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints); 925bdd41206SAndrew Thompson 926760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)id; 927bdd41206SAndrew Thompson 928bdd41206SAndrew Thompson temp = ep_curr; 929bdd41206SAndrew Thompson 930bdd41206SAndrew Thompson /* iterate all the endpoint descriptors */ 931a593f6b8SAndrew Thompson while ((ed = usb_edesc_foreach(udev->cdesc, ed))) { 932bdd41206SAndrew Thompson 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++) { 118702ac6454SAndrew Thompson 1188ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 1189ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 119002ac6454SAndrew Thompson continue; 119102ac6454SAndrew Thompson } 119202ac6454SAndrew Thompson /* simulate a clear stall from the peer */ 1193bd73b187SAlfred Perlstein usbd_set_endpoint_stall(udev, ep, 0); 119402ac6454SAndrew Thompson } 119502ac6454SAndrew Thompson return (0); 119602ac6454SAndrew Thompson } 119702ac6454SAndrew Thompson 119802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1199a593f6b8SAndrew Thompson * usb_detach_device_sub 120002ac6454SAndrew Thompson * 120102ac6454SAndrew Thompson * This function will try to detach an USB device. If it fails a panic 120202ac6454SAndrew Thompson * will result. 1203bdd41206SAndrew Thompson * 1204bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 120502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 120602ac6454SAndrew Thompson static void 1207a593f6b8SAndrew Thompson usb_detach_device_sub(struct usb_device *udev, device_t *ppdev, 12088427ed84SHans Petter Selasky char **ppnpinfo, uint8_t flag) 120902ac6454SAndrew Thompson { 121002ac6454SAndrew Thompson device_t dev; 12118427ed84SHans Petter Selasky char *pnpinfo; 121202ac6454SAndrew Thompson int err; 121302ac6454SAndrew Thompson 1214d88688c7SAndrew Thompson dev = *ppdev; 1215d88688c7SAndrew Thompson if (dev) { 121602ac6454SAndrew Thompson /* 121702ac6454SAndrew Thompson * NOTE: It is important to clear "*ppdev" before deleting 121802ac6454SAndrew Thompson * the child due to some device methods being called late 121902ac6454SAndrew Thompson * during the delete process ! 122002ac6454SAndrew Thompson */ 122102ac6454SAndrew Thompson *ppdev = NULL; 122202ac6454SAndrew Thompson 1223cabe79d9SMarius Strobl if (!rebooting) { 122402ac6454SAndrew Thompson device_printf(dev, "at %s, port %d, addr %d " 122502ac6454SAndrew Thompson "(disconnected)\n", 122602ac6454SAndrew Thompson device_get_nameunit(udev->parent_dev), 122702ac6454SAndrew Thompson udev->port_no, udev->address); 1228cabe79d9SMarius Strobl } 122902ac6454SAndrew Thompson 123002ac6454SAndrew Thompson if (device_is_attached(dev)) { 1231ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 123202ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 123302ac6454SAndrew Thompson if (err) { 1234767cb2e2SAndrew Thompson device_printf(dev, "Resume failed\n"); 123502ac6454SAndrew Thompson } 123602ac6454SAndrew Thompson } 123702ac6454SAndrew Thompson } 1238d3bf5efcSHans Petter Selasky /* detach and delete child */ 123902ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 124002ac6454SAndrew Thompson goto error; 124102ac6454SAndrew Thompson } 124202ac6454SAndrew Thompson } 12438427ed84SHans Petter Selasky 12448427ed84SHans Petter Selasky pnpinfo = *ppnpinfo; 12458427ed84SHans Petter Selasky if (pnpinfo != NULL) { 12468427ed84SHans Petter Selasky *ppnpinfo = NULL; 12478427ed84SHans Petter Selasky free(pnpinfo, M_USBDEV); 12488427ed84SHans Petter Selasky } 124902ac6454SAndrew Thompson return; 125002ac6454SAndrew Thompson 125102ac6454SAndrew Thompson error: 125202ac6454SAndrew Thompson /* Detach is not allowed to fail in the USB world */ 12538427ed84SHans Petter Selasky panic("usb_detach_device_sub: A USB driver would not detach\n"); 125402ac6454SAndrew Thompson } 125502ac6454SAndrew Thompson 125602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1257a593f6b8SAndrew Thompson * usb_detach_device 125802ac6454SAndrew Thompson * 125902ac6454SAndrew Thompson * The following function will detach the matching interfaces. 126002ac6454SAndrew Thompson * This function is NULL safe. 1261bdd41206SAndrew Thompson * 1262bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 126302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 126402ac6454SAndrew Thompson void 1265a593f6b8SAndrew Thompson usb_detach_device(struct usb_device *udev, uint8_t iface_index, 1266bdd41206SAndrew Thompson uint8_t flag) 126702ac6454SAndrew Thompson { 1268760bc48eSAndrew Thompson struct usb_interface *iface; 126902ac6454SAndrew Thompson uint8_t i; 127002ac6454SAndrew Thompson 127102ac6454SAndrew Thompson if (udev == NULL) { 127202ac6454SAndrew Thompson /* nothing to do */ 127302ac6454SAndrew Thompson return; 127402ac6454SAndrew Thompson } 127502ac6454SAndrew Thompson DPRINTFN(4, "udev=%p\n", udev); 127602ac6454SAndrew Thompson 127791cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 127802ac6454SAndrew Thompson 127902ac6454SAndrew Thompson /* 128002ac6454SAndrew Thompson * First detach the child to give the child's detach routine a 128102ac6454SAndrew Thompson * chance to detach the sub-devices in the correct order. 128202ac6454SAndrew Thompson * Then delete the child using "device_delete_child()" which 128302ac6454SAndrew Thompson * will detach all sub-devices from the bottom and upwards! 128402ac6454SAndrew Thompson */ 128502ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 128602ac6454SAndrew Thompson i = iface_index; 128702ac6454SAndrew Thompson iface_index = i + 1; 128802ac6454SAndrew Thompson } else { 128902ac6454SAndrew Thompson i = 0; 129002ac6454SAndrew Thompson iface_index = USB_IFACE_MAX; 129102ac6454SAndrew Thompson } 129202ac6454SAndrew Thompson 129302ac6454SAndrew Thompson /* do the detach */ 129402ac6454SAndrew Thompson 129502ac6454SAndrew Thompson for (; i != iface_index; i++) { 129602ac6454SAndrew Thompson 1297a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 129802ac6454SAndrew Thompson if (iface == NULL) { 129902ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 130002ac6454SAndrew Thompson break; 130102ac6454SAndrew Thompson } 13028427ed84SHans Petter Selasky usb_detach_device_sub(udev, &iface->subdev, 13038427ed84SHans Petter Selasky &iface->pnpinfo, flag); 130402ac6454SAndrew Thompson } 130502ac6454SAndrew Thompson } 130602ac6454SAndrew Thompson 130702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1308a593f6b8SAndrew Thompson * usb_probe_and_attach_sub 130902ac6454SAndrew Thompson * 131002ac6454SAndrew Thompson * Returns: 131102ac6454SAndrew Thompson * 0: Success 131202ac6454SAndrew Thompson * Else: Failure 131302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 131402ac6454SAndrew Thompson static uint8_t 1315a593f6b8SAndrew Thompson usb_probe_and_attach_sub(struct usb_device *udev, 1316760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 131702ac6454SAndrew Thompson { 1318760bc48eSAndrew Thompson struct usb_interface *iface; 131902ac6454SAndrew Thompson device_t dev; 132002ac6454SAndrew Thompson int err; 132102ac6454SAndrew Thompson 132202ac6454SAndrew Thompson iface = uaa->iface; 132302ac6454SAndrew Thompson if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 132402ac6454SAndrew Thompson /* leave interface alone */ 132502ac6454SAndrew Thompson return (0); 132602ac6454SAndrew Thompson } 132702ac6454SAndrew Thompson dev = iface->subdev; 132802ac6454SAndrew Thompson if (dev) { 132902ac6454SAndrew Thompson 133002ac6454SAndrew Thompson /* clean up after module unload */ 133102ac6454SAndrew Thompson 133202ac6454SAndrew Thompson if (device_is_attached(dev)) { 133302ac6454SAndrew Thompson /* already a device there */ 133402ac6454SAndrew Thompson return (0); 133502ac6454SAndrew Thompson } 133602ac6454SAndrew Thompson /* clear "iface->subdev" as early as possible */ 133702ac6454SAndrew Thompson 133802ac6454SAndrew Thompson iface->subdev = NULL; 133902ac6454SAndrew Thompson 134002ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 134102ac6454SAndrew Thompson 134202ac6454SAndrew Thompson /* 134302ac6454SAndrew Thompson * Panic here, else one can get a double call 134402ac6454SAndrew Thompson * to device_detach(). USB devices should 134502ac6454SAndrew Thompson * never fail on detach! 134602ac6454SAndrew Thompson */ 1347767cb2e2SAndrew Thompson panic("device_delete_child() failed\n"); 134802ac6454SAndrew Thompson } 134902ac6454SAndrew Thompson } 135002ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 135102ac6454SAndrew Thompson 135202ac6454SAndrew Thompson /* create a new child */ 135302ac6454SAndrew Thompson uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1); 135402ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 135502ac6454SAndrew Thompson device_printf(udev->parent_dev, 1356767cb2e2SAndrew Thompson "Device creation failed\n"); 135702ac6454SAndrew Thompson return (1); /* failure */ 135802ac6454SAndrew Thompson } 135902ac6454SAndrew Thompson device_set_ivars(uaa->temp_dev, uaa); 136002ac6454SAndrew Thompson device_quiet(uaa->temp_dev); 136102ac6454SAndrew Thompson } 136202ac6454SAndrew Thompson /* 136302ac6454SAndrew Thompson * Set "subdev" before probe and attach so that "devd" gets 136402ac6454SAndrew Thompson * the information it needs. 136502ac6454SAndrew Thompson */ 136602ac6454SAndrew Thompson iface->subdev = uaa->temp_dev; 136702ac6454SAndrew Thompson 136802ac6454SAndrew Thompson if (device_probe_and_attach(iface->subdev) == 0) { 136902ac6454SAndrew Thompson /* 137002ac6454SAndrew Thompson * The USB attach arguments are only available during probe 137102ac6454SAndrew Thompson * and attach ! 137202ac6454SAndrew Thompson */ 137302ac6454SAndrew Thompson uaa->temp_dev = NULL; 137402ac6454SAndrew Thompson device_set_ivars(iface->subdev, NULL); 137502ac6454SAndrew Thompson 1376ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 137702ac6454SAndrew Thompson err = DEVICE_SUSPEND(iface->subdev); 137888b4e0abSAndrew Thompson if (err) 137902ac6454SAndrew Thompson device_printf(iface->subdev, "Suspend failed\n"); 138002ac6454SAndrew Thompson } 138102ac6454SAndrew Thompson return (0); /* success */ 138202ac6454SAndrew Thompson } else { 138302ac6454SAndrew Thompson /* No USB driver found */ 138402ac6454SAndrew Thompson iface->subdev = NULL; 138502ac6454SAndrew Thompson } 138602ac6454SAndrew Thompson return (1); /* failure */ 138702ac6454SAndrew Thompson } 138802ac6454SAndrew Thompson 138902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1390a593f6b8SAndrew Thompson * usbd_set_parent_iface 139102ac6454SAndrew Thompson * 139202ac6454SAndrew Thompson * Using this function will lock the alternate interface setting on an 139302ac6454SAndrew Thompson * interface. It is typically used for multi interface drivers. In USB 139402ac6454SAndrew Thompson * device side mode it is assumed that the alternate interfaces all 139502ac6454SAndrew Thompson * have the same endpoint descriptors. The default parent index value 139602ac6454SAndrew Thompson * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 139702ac6454SAndrew Thompson * locked. 139802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 139902ac6454SAndrew Thompson void 1400a593f6b8SAndrew Thompson usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index, 140102ac6454SAndrew Thompson uint8_t parent_index) 140202ac6454SAndrew Thompson { 1403760bc48eSAndrew Thompson struct usb_interface *iface; 140402ac6454SAndrew Thompson 14055e055201SHans Petter Selasky if (udev == NULL || iface_index == parent_index) { 1406d9073c1eSHans Petter Selasky /* nothing to do */ 1407d9073c1eSHans Petter Selasky return; 140802ac6454SAndrew Thompson } 1409d9073c1eSHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 1410d9073c1eSHans Petter Selasky if (iface != NULL) 1411d9073c1eSHans Petter Selasky iface->parent_iface_index = parent_index; 141202ac6454SAndrew Thompson } 141302ac6454SAndrew Thompson 141402ac6454SAndrew Thompson static void 1415a593f6b8SAndrew Thompson usb_init_attach_arg(struct usb_device *udev, 1416760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 141702ac6454SAndrew Thompson { 1418d46dc4adSHans Petter Selasky memset(uaa, 0, sizeof(*uaa)); 141902ac6454SAndrew Thompson 142002ac6454SAndrew Thompson uaa->device = udev; 1421f29a0724SAndrew Thompson uaa->usb_mode = udev->flags.usb_mode; 142202ac6454SAndrew Thompson uaa->port = udev->port_no; 14232a4c6157SAndrew Thompson uaa->dev_state = UAA_DEV_READY; 142402ac6454SAndrew Thompson 142502ac6454SAndrew Thompson uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 142602ac6454SAndrew Thompson uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 142702ac6454SAndrew Thompson uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 142802ac6454SAndrew Thompson uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 142902ac6454SAndrew Thompson uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 143002ac6454SAndrew Thompson uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 143102ac6454SAndrew Thompson uaa->info.bConfigIndex = udev->curr_config_index; 143202ac6454SAndrew Thompson uaa->info.bConfigNum = udev->curr_config_no; 143302ac6454SAndrew Thompson } 143402ac6454SAndrew Thompson 143502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1436a593f6b8SAndrew Thompson * usb_probe_and_attach 143702ac6454SAndrew Thompson * 143802ac6454SAndrew Thompson * This function is called from "uhub_explore_sub()", 1439a593f6b8SAndrew Thompson * "usb_handle_set_config()" and "usb_handle_request()". 144002ac6454SAndrew Thompson * 144102ac6454SAndrew Thompson * Returns: 144202ac6454SAndrew Thompson * 0: Success 144302ac6454SAndrew Thompson * Else: A control transfer failed 144402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1445e0a69b51SAndrew Thompson usb_error_t 1446a593f6b8SAndrew Thompson usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index) 144702ac6454SAndrew Thompson { 1448760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1449760bc48eSAndrew Thompson struct usb_interface *iface; 145002ac6454SAndrew Thompson uint8_t i; 145102ac6454SAndrew Thompson uint8_t j; 145202ac6454SAndrew Thompson uint8_t do_unlock; 145302ac6454SAndrew Thompson 145402ac6454SAndrew Thompson if (udev == NULL) { 145502ac6454SAndrew Thompson DPRINTF("udev == NULL\n"); 145602ac6454SAndrew Thompson return (USB_ERR_INVAL); 145702ac6454SAndrew Thompson } 14586950c75fSHans Petter Selasky /* Prevent re-enumeration */ 14596950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 146002ac6454SAndrew Thompson 146102ac6454SAndrew Thompson if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 146202ac6454SAndrew Thompson /* do nothing - no configuration has been set */ 146302ac6454SAndrew Thompson goto done; 146402ac6454SAndrew Thompson } 146502ac6454SAndrew Thompson /* setup USB attach arguments */ 146602ac6454SAndrew Thompson 1467a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 146802ac6454SAndrew Thompson 1469a6b60150SHans Petter Selasky /* 1470a6b60150SHans Petter Selasky * If the whole USB device is targeted, invoke the USB event 1471a6b60150SHans Petter Selasky * handler(s): 1472a6b60150SHans Petter Selasky */ 1473a6b60150SHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 1474a6b60150SHans Petter Selasky 1475415bcd89SHans Petter Selasky if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 && 1476415bcd89SHans Petter Selasky usb_dymo_eject(udev, 0) == 0) { 1477415bcd89SHans Petter Selasky /* success, mark the udev as disappearing */ 1478415bcd89SHans Petter Selasky uaa.dev_state = UAA_DEV_EJECTING; 1479415bcd89SHans Petter Selasky } 1480415bcd89SHans Petter Selasky 1481a6b60150SHans Petter Selasky EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa); 1482a6b60150SHans Petter Selasky 1483a6b60150SHans Petter Selasky if (uaa.dev_state != UAA_DEV_READY) { 1484a6b60150SHans Petter Selasky /* leave device unconfigured */ 1485a6b60150SHans Petter Selasky usb_unconfigure(udev, 0); 1486a6b60150SHans Petter Selasky goto done; 1487a6b60150SHans Petter Selasky } 1488a6b60150SHans Petter Selasky } 1489a6b60150SHans Petter Selasky 149002ac6454SAndrew Thompson /* Check if only one interface should be probed: */ 149102ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 149202ac6454SAndrew Thompson i = iface_index; 149302ac6454SAndrew Thompson j = i + 1; 149402ac6454SAndrew Thompson } else { 149502ac6454SAndrew Thompson i = 0; 149602ac6454SAndrew Thompson j = USB_IFACE_MAX; 149702ac6454SAndrew Thompson } 149802ac6454SAndrew Thompson 149902ac6454SAndrew Thompson /* Do the probe and attach */ 150002ac6454SAndrew Thompson for (; i != j; i++) { 150102ac6454SAndrew Thompson 1502a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 150302ac6454SAndrew Thompson if (iface == NULL) { 150402ac6454SAndrew Thompson /* 150502ac6454SAndrew Thompson * Looks like the end of the USB 150602ac6454SAndrew Thompson * interfaces ! 150702ac6454SAndrew Thompson */ 150802ac6454SAndrew Thompson DPRINTFN(2, "end of interfaces " 150902ac6454SAndrew Thompson "at %u\n", i); 151002ac6454SAndrew Thompson break; 151102ac6454SAndrew Thompson } 151202ac6454SAndrew Thompson if (iface->idesc == NULL) { 151302ac6454SAndrew Thompson /* no interface descriptor */ 151402ac6454SAndrew Thompson continue; 151502ac6454SAndrew Thompson } 151602ac6454SAndrew Thompson uaa.iface = iface; 151702ac6454SAndrew Thompson 151802ac6454SAndrew Thompson uaa.info.bInterfaceClass = 151902ac6454SAndrew Thompson iface->idesc->bInterfaceClass; 152002ac6454SAndrew Thompson uaa.info.bInterfaceSubClass = 152102ac6454SAndrew Thompson iface->idesc->bInterfaceSubClass; 152202ac6454SAndrew Thompson uaa.info.bInterfaceProtocol = 152302ac6454SAndrew Thompson iface->idesc->bInterfaceProtocol; 152402ac6454SAndrew Thompson uaa.info.bIfaceIndex = i; 152502ac6454SAndrew Thompson uaa.info.bIfaceNum = 152602ac6454SAndrew Thompson iface->idesc->bInterfaceNumber; 1527bd73b187SAlfred Perlstein uaa.driver_info = 0; /* reset driver_info */ 152802ac6454SAndrew Thompson 152902ac6454SAndrew Thompson DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 153002ac6454SAndrew Thompson uaa.info.bInterfaceClass, 153102ac6454SAndrew Thompson uaa.info.bInterfaceSubClass, 153202ac6454SAndrew Thompson uaa.info.bInterfaceProtocol, 153302ac6454SAndrew Thompson uaa.info.bIfaceIndex, 153402ac6454SAndrew Thompson uaa.info.bIfaceNum); 153502ac6454SAndrew Thompson 1536d43ffe94SHans Petter Selasky usb_probe_and_attach_sub(udev, &uaa); 153702ac6454SAndrew Thompson 1538d43ffe94SHans Petter Selasky /* 1539d43ffe94SHans Petter Selasky * Remove the leftover child, if any, to enforce that 1540d43ffe94SHans Petter Selasky * a new nomatch devd event is generated for the next 1541d43ffe94SHans Petter Selasky * interface if no driver is found: 1542d43ffe94SHans Petter Selasky */ 1543d43ffe94SHans Petter Selasky if (uaa.temp_dev == NULL) 1544d43ffe94SHans Petter Selasky continue; 1545d43ffe94SHans Petter Selasky if (device_delete_child(udev->parent_dev, uaa.temp_dev)) 1546767cb2e2SAndrew Thompson DPRINTFN(0, "device delete child failed\n"); 1547d43ffe94SHans Petter Selasky uaa.temp_dev = NULL; 154802ac6454SAndrew Thompson } 154902ac6454SAndrew Thompson done: 1550cb18f7d1SAlfred Perlstein if (do_unlock) 1551cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 155202ac6454SAndrew Thompson return (0); 155302ac6454SAndrew Thompson } 155402ac6454SAndrew Thompson 155502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1556a593f6b8SAndrew Thompson * usb_suspend_resume_sub 155702ac6454SAndrew Thompson * 155802ac6454SAndrew Thompson * This function is called when the suspend or resume methods should 155902ac6454SAndrew Thompson * be executed on an USB device. 156002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 156102ac6454SAndrew Thompson static void 1562a593f6b8SAndrew Thompson usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend) 156302ac6454SAndrew Thompson { 156402ac6454SAndrew Thompson int err; 156502ac6454SAndrew Thompson 156602ac6454SAndrew Thompson if (dev == NULL) { 156702ac6454SAndrew Thompson return; 156802ac6454SAndrew Thompson } 156902ac6454SAndrew Thompson if (!device_is_attached(dev)) { 157002ac6454SAndrew Thompson return; 157102ac6454SAndrew Thompson } 157202ac6454SAndrew Thompson if (do_suspend) { 157302ac6454SAndrew Thompson err = DEVICE_SUSPEND(dev); 157402ac6454SAndrew Thompson } else { 157502ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 157602ac6454SAndrew Thompson } 157702ac6454SAndrew Thompson if (err) { 1578767cb2e2SAndrew Thompson device_printf(dev, "%s failed\n", 157902ac6454SAndrew Thompson do_suspend ? "Suspend" : "Resume"); 158002ac6454SAndrew Thompson } 158102ac6454SAndrew Thompson } 158202ac6454SAndrew Thompson 158302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1584a593f6b8SAndrew Thompson * usb_suspend_resume 158502ac6454SAndrew Thompson * 158602ac6454SAndrew Thompson * The following function will suspend or resume the USB device. 158702ac6454SAndrew Thompson * 158802ac6454SAndrew Thompson * Returns: 158902ac6454SAndrew Thompson * 0: Success 159002ac6454SAndrew Thompson * Else: Failure 159102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1592e0a69b51SAndrew Thompson usb_error_t 1593a593f6b8SAndrew Thompson usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend) 159402ac6454SAndrew Thompson { 1595760bc48eSAndrew Thompson struct usb_interface *iface; 159602ac6454SAndrew Thompson uint8_t i; 159702ac6454SAndrew Thompson 159802ac6454SAndrew Thompson if (udev == NULL) { 159902ac6454SAndrew Thompson /* nothing to do */ 160002ac6454SAndrew Thompson return (0); 160102ac6454SAndrew Thompson } 160202ac6454SAndrew Thompson DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 160302ac6454SAndrew Thompson 16042df1e9a6SAndrew Thompson sx_assert(&udev->sr_sx, SA_LOCKED); 160502ac6454SAndrew Thompson 160602ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 160702ac6454SAndrew Thompson /* filter the suspend events */ 1608ec8f3127SAndrew Thompson if (udev->flags.peer_suspended == do_suspend) { 160902ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 161002ac6454SAndrew Thompson /* nothing to do */ 161102ac6454SAndrew Thompson return (0); 161202ac6454SAndrew Thompson } 1613ec8f3127SAndrew Thompson udev->flags.peer_suspended = do_suspend; 161402ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 161502ac6454SAndrew Thompson 161602ac6454SAndrew Thompson /* do the suspend or resume */ 161702ac6454SAndrew Thompson 161802ac6454SAndrew Thompson for (i = 0; i != USB_IFACE_MAX; i++) { 161902ac6454SAndrew Thompson 1620a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 162102ac6454SAndrew Thompson if (iface == NULL) { 162202ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 162302ac6454SAndrew Thompson break; 162402ac6454SAndrew Thompson } 1625a593f6b8SAndrew Thompson usb_suspend_resume_sub(udev, iface->subdev, do_suspend); 162602ac6454SAndrew Thompson } 162702ac6454SAndrew Thompson return (0); 162802ac6454SAndrew Thompson } 162902ac6454SAndrew Thompson 163002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1631a593f6b8SAndrew Thompson * usbd_clear_stall_proc 163202ac6454SAndrew Thompson * 163302ac6454SAndrew Thompson * This function performs generic USB clear stall operations. 163402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 163502ac6454SAndrew Thompson static void 1636a593f6b8SAndrew Thompson usbd_clear_stall_proc(struct usb_proc_msg *_pm) 163702ac6454SAndrew Thompson { 1638a0d53e0bSHans Petter Selasky struct usb_udev_msg *pm = (void *)_pm; 1639760bc48eSAndrew Thompson struct usb_device *udev = pm->udev; 164002ac6454SAndrew Thompson 164102ac6454SAndrew Thompson /* Change lock */ 164202ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 16430eb8d462SHans Petter Selasky USB_MTX_LOCK(&udev->device_mtx); 164402ac6454SAndrew Thompson 164502ac6454SAndrew Thompson /* Start clear stall callback */ 16465b3bb704SAndrew Thompson usbd_transfer_start(udev->ctrl_xfer[1]); 164702ac6454SAndrew Thompson 164802ac6454SAndrew Thompson /* Change lock */ 16490eb8d462SHans Petter Selasky USB_MTX_UNLOCK(&udev->device_mtx); 165002ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 165102ac6454SAndrew Thompson } 165202ac6454SAndrew Thompson 165302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1654f54ab96dSHans Petter Selasky * usb_get_langid 1655f54ab96dSHans Petter Selasky * 1656f54ab96dSHans Petter Selasky * This function tries to figure out the USB string language to use. 1657f54ab96dSHans Petter Selasky *------------------------------------------------------------------------*/ 1658f54ab96dSHans Petter Selasky void 1659f54ab96dSHans Petter Selasky usb_get_langid(struct usb_device *udev) 1660f54ab96dSHans Petter Selasky { 1661f54ab96dSHans Petter Selasky uint8_t *scratch_ptr; 1662f54ab96dSHans Petter Selasky uint8_t do_unlock; 1663f54ab96dSHans Petter Selasky int err; 1664f54ab96dSHans Petter Selasky 1665f54ab96dSHans Petter Selasky /* 1666f54ab96dSHans Petter Selasky * Workaround for buggy USB devices. 1667f54ab96dSHans Petter Selasky * 1668f54ab96dSHans Petter Selasky * It appears that some string-less USB chips will crash and 1669f54ab96dSHans Petter Selasky * disappear if any attempts are made to read any string 1670f54ab96dSHans Petter Selasky * descriptors. 1671f54ab96dSHans Petter Selasky * 1672f54ab96dSHans Petter Selasky * Try to detect such chips by checking the strings in the USB 1673f54ab96dSHans Petter Selasky * device descriptor. If no strings are present there we 1674f54ab96dSHans Petter Selasky * simply disable all USB strings. 1675f54ab96dSHans Petter Selasky */ 1676f54ab96dSHans Petter Selasky 1677f54ab96dSHans Petter Selasky /* Protect scratch area */ 1678f54ab96dSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 1679f54ab96dSHans Petter Selasky 1680f54ab96dSHans Petter Selasky scratch_ptr = udev->scratch.data; 1681f54ab96dSHans Petter Selasky 1682f54ab96dSHans Petter Selasky if (udev->flags.no_strings) { 1683f54ab96dSHans Petter Selasky err = USB_ERR_INVAL; 1684f54ab96dSHans Petter Selasky } else if (udev->ddesc.iManufacturer || 1685f54ab96dSHans Petter Selasky udev->ddesc.iProduct || 1686f54ab96dSHans Petter Selasky udev->ddesc.iSerialNumber) { 1687f54ab96dSHans Petter Selasky /* read out the language ID string */ 1688f54ab96dSHans Petter Selasky err = usbd_req_get_string_desc(udev, NULL, 1689f54ab96dSHans Petter Selasky (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE); 1690f54ab96dSHans Petter Selasky } else { 1691f54ab96dSHans Petter Selasky err = USB_ERR_INVAL; 1692f54ab96dSHans Petter Selasky } 1693f54ab96dSHans Petter Selasky 1694f54ab96dSHans Petter Selasky if (err || (scratch_ptr[0] < 4)) { 1695f54ab96dSHans Petter Selasky udev->flags.no_strings = 1; 1696f54ab96dSHans Petter Selasky } else { 1697f54ab96dSHans Petter Selasky uint16_t langid; 1698f54ab96dSHans Petter Selasky uint16_t pref; 1699f54ab96dSHans Petter Selasky uint16_t mask; 1700f54ab96dSHans Petter Selasky uint8_t x; 1701f54ab96dSHans Petter Selasky 1702f54ab96dSHans Petter Selasky /* load preferred value and mask */ 1703f54ab96dSHans Petter Selasky pref = usb_lang_id; 1704f54ab96dSHans Petter Selasky mask = usb_lang_mask; 1705f54ab96dSHans Petter Selasky 1706f54ab96dSHans Petter Selasky /* align length correctly */ 1707f54ab96dSHans Petter Selasky scratch_ptr[0] &= ~1U; 1708f54ab96dSHans Petter Selasky 1709f54ab96dSHans Petter Selasky /* fix compiler warning */ 1710f54ab96dSHans Petter Selasky langid = 0; 1711f54ab96dSHans Petter Selasky 1712f54ab96dSHans Petter Selasky /* search for preferred language */ 1713f54ab96dSHans Petter Selasky for (x = 2; x < scratch_ptr[0]; x += 2) { 1714f54ab96dSHans Petter Selasky langid = UGETW(scratch_ptr + x); 1715f54ab96dSHans Petter Selasky if ((langid & mask) == pref) 1716f54ab96dSHans Petter Selasky break; 1717f54ab96dSHans Petter Selasky } 1718f54ab96dSHans Petter Selasky if (x >= scratch_ptr[0]) { 1719f54ab96dSHans Petter Selasky /* pick the first language as the default */ 1720f54ab96dSHans Petter Selasky DPRINTFN(1, "Using first language\n"); 1721f54ab96dSHans Petter Selasky langid = UGETW(scratch_ptr + 2); 1722f54ab96dSHans Petter Selasky } 1723f54ab96dSHans Petter Selasky 1724f54ab96dSHans Petter Selasky DPRINTFN(1, "Language selected: 0x%04x\n", langid); 1725f54ab96dSHans Petter Selasky udev->langid = langid; 1726f54ab96dSHans Petter Selasky } 1727f54ab96dSHans Petter Selasky 1728f54ab96dSHans Petter Selasky if (do_unlock) 1729f54ab96dSHans Petter Selasky usbd_ctrl_unlock(udev); 1730f54ab96dSHans Petter Selasky } 1731f54ab96dSHans Petter Selasky 1732f54ab96dSHans Petter Selasky /*------------------------------------------------------------------------* 1733a593f6b8SAndrew Thompson * usb_alloc_device 173402ac6454SAndrew Thompson * 173502ac6454SAndrew Thompson * This function allocates a new USB device. This function is called 173602ac6454SAndrew Thompson * when a new device has been put in the powered state, but not yet in 173702ac6454SAndrew Thompson * the addressed state. Get initial descriptor, set the address, get 173802ac6454SAndrew Thompson * full descriptor and get strings. 173902ac6454SAndrew Thompson * 174002ac6454SAndrew Thompson * Return values: 174102ac6454SAndrew Thompson * 0: Failure 174202ac6454SAndrew Thompson * Else: Success 174302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1744760bc48eSAndrew Thompson struct usb_device * 1745a593f6b8SAndrew Thompson usb_alloc_device(device_t parent_dev, struct usb_bus *bus, 1746760bc48eSAndrew Thompson struct usb_device *parent_hub, uint8_t depth, uint8_t port_index, 17478d2dd5ddSAndrew Thompson uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode) 174802ac6454SAndrew Thompson { 1749760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1750760bc48eSAndrew Thompson struct usb_device *udev; 1751760bc48eSAndrew Thompson struct usb_device *adev; 1752760bc48eSAndrew Thompson struct usb_device *hub; 1753e0a69b51SAndrew Thompson usb_error_t err; 175402ac6454SAndrew Thompson uint8_t device_index; 17552a4c6157SAndrew Thompson uint8_t config_index; 17562a4c6157SAndrew Thompson uint8_t config_quirk; 17572a4c6157SAndrew Thompson uint8_t set_config_failed; 175802ac6454SAndrew Thompson 175902ac6454SAndrew Thompson DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1760f29a0724SAndrew Thompson "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n", 176102ac6454SAndrew Thompson parent_dev, bus, parent_hub, depth, port_index, port_no, 1762f29a0724SAndrew Thompson speed, mode); 176302ac6454SAndrew Thompson 176402ac6454SAndrew Thompson /* 176502ac6454SAndrew Thompson * Find an unused device index. In USB Host mode this is the 176602ac6454SAndrew Thompson * same as the device address. 176702ac6454SAndrew Thompson * 176802ac6454SAndrew Thompson * Device index zero is not used and device index 1 should 176902ac6454SAndrew Thompson * always be the root hub. 177002ac6454SAndrew Thompson */ 177102ac6454SAndrew Thompson for (device_index = USB_ROOT_HUB_ADDR; 177202ac6454SAndrew Thompson (device_index != bus->devices_max) && 177302ac6454SAndrew Thompson (bus->devices[device_index] != NULL); 177402ac6454SAndrew Thompson device_index++) /* nop */; 177502ac6454SAndrew Thompson 177602ac6454SAndrew Thompson if (device_index == bus->devices_max) { 177702ac6454SAndrew Thompson device_printf(bus->bdev, 1778767cb2e2SAndrew Thompson "No free USB device index for new device\n"); 177902ac6454SAndrew Thompson return (NULL); 178002ac6454SAndrew Thompson } 178102ac6454SAndrew Thompson 178202ac6454SAndrew Thompson if (depth > 0x10) { 178302ac6454SAndrew Thompson device_printf(bus->bdev, 1784767cb2e2SAndrew Thompson "Invalid device depth\n"); 178502ac6454SAndrew Thompson return (NULL); 178602ac6454SAndrew Thompson } 178702ac6454SAndrew Thompson udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 1788*94140f47SMark Johnston #if (USB_HAVE_MALLOC_WAITOK == 0) 178902ac6454SAndrew Thompson if (udev == NULL) { 179002ac6454SAndrew Thompson return (NULL); 179102ac6454SAndrew Thompson } 1792*94140f47SMark Johnston #endif 179302ac6454SAndrew Thompson /* initialise our SX-lock */ 179491cd9240SAndrew Thompson sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK); 1795a6b60150SHans Petter Selasky sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS); 179664cb5e2aSHans Petter Selasky sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK); 179702ac6454SAndrew Thompson 179891cd9240SAndrew Thompson cv_init(&udev->ctrlreq_cv, "WCTRL"); 179991cd9240SAndrew Thompson cv_init(&udev->ref_cv, "UGONE"); 180002ac6454SAndrew Thompson 180102ac6454SAndrew Thompson /* initialise our mutex */ 180291cd9240SAndrew Thompson mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF); 180302ac6454SAndrew Thompson 180402ac6454SAndrew Thompson /* initialise generic clear stall */ 1805a593f6b8SAndrew Thompson udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc; 180602ac6454SAndrew Thompson udev->cs_msg[0].udev = udev; 1807a593f6b8SAndrew Thompson udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc; 180802ac6454SAndrew Thompson udev->cs_msg[1].udev = udev; 180902ac6454SAndrew Thompson 181002ac6454SAndrew Thompson /* initialise some USB device fields */ 181102ac6454SAndrew Thompson udev->parent_hub = parent_hub; 181202ac6454SAndrew Thompson udev->parent_dev = parent_dev; 181302ac6454SAndrew Thompson udev->port_index = port_index; 181402ac6454SAndrew Thompson udev->port_no = port_no; 181502ac6454SAndrew Thompson udev->depth = depth; 181602ac6454SAndrew Thompson udev->bus = bus; 181702ac6454SAndrew Thompson udev->address = USB_START_ADDR; /* default value */ 1818e0a69b51SAndrew Thompson udev->plugtime = (usb_ticks_t)ticks; 181902ac6454SAndrew Thompson /* 182002ac6454SAndrew Thompson * We need to force the power mode to "on" because there are plenty 182102ac6454SAndrew Thompson * of USB devices out there that do not work very well with 182202ac6454SAndrew Thompson * automatic suspend and resume! 182302ac6454SAndrew Thompson */ 1824d2d71ce7SAndrew Thompson udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON); 182502ac6454SAndrew Thompson udev->pwr_save.last_xfer_time = ticks; 182602ac6454SAndrew Thompson /* we are not ready yet */ 182702ac6454SAndrew Thompson udev->refcount = 1; 182802ac6454SAndrew Thompson 182902ac6454SAndrew Thompson /* set up default endpoint descriptor */ 18305b3bb704SAndrew Thompson udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc); 18315b3bb704SAndrew Thompson udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT; 18325b3bb704SAndrew Thompson udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 18335b3bb704SAndrew Thompson udev->ctrl_ep_desc.bmAttributes = UE_CONTROL; 18345b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 18355b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[1] = 0; 18365b3bb704SAndrew Thompson udev->ctrl_ep_desc.bInterval = 0; 1837963169b4SHans Petter Selasky 1838963169b4SHans Petter Selasky /* set up default endpoint companion descriptor */ 1839963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc); 1840963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP; 1841963169b4SHans Petter Selasky 184202ac6454SAndrew Thompson udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 184302ac6454SAndrew Thompson 184402ac6454SAndrew Thompson udev->speed = speed; 1845f29a0724SAndrew Thompson udev->flags.usb_mode = mode; 184602ac6454SAndrew Thompson 184702ac6454SAndrew Thompson /* search for our High Speed USB HUB, if any */ 184802ac6454SAndrew Thompson 184902ac6454SAndrew Thompson adev = udev; 185002ac6454SAndrew Thompson hub = udev->parent_hub; 185102ac6454SAndrew Thompson 185202ac6454SAndrew Thompson while (hub) { 185302ac6454SAndrew Thompson if (hub->speed == USB_SPEED_HIGH) { 185402ac6454SAndrew Thompson udev->hs_hub_addr = hub->address; 1855495f25ceSAndrew Thompson udev->parent_hs_hub = hub; 185602ac6454SAndrew Thompson udev->hs_port_no = adev->port_no; 185702ac6454SAndrew Thompson break; 185802ac6454SAndrew Thompson } 185902ac6454SAndrew Thompson adev = hub; 186002ac6454SAndrew Thompson hub = hub->parent_hub; 186102ac6454SAndrew Thompson } 186202ac6454SAndrew Thompson 1863ae60fdfbSAndrew Thompson /* init the default endpoint */ 1864a593f6b8SAndrew Thompson usb_init_endpoint(udev, 0, 18655b3bb704SAndrew Thompson &udev->ctrl_ep_desc, 1866963169b4SHans Petter Selasky &udev->ctrl_ep_comp_desc, 18675b3bb704SAndrew Thompson &udev->ctrl_ep); 186802ac6454SAndrew Thompson 186902ac6454SAndrew Thompson /* set device index */ 187002ac6454SAndrew Thompson udev->device_index = device_index; 187102ac6454SAndrew Thompson 18728755859aSAndrew Thompson #if USB_HAVE_UGEN 18730f6e8f93SAndrew Thompson /* Create ugen name */ 1874ee3e3ff5SAndrew Thompson snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1875ee3e3ff5SAndrew Thompson USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1876ee3e3ff5SAndrew Thompson device_index); 18778755859aSAndrew Thompson LIST_INIT(&udev->pd_list); 18788755859aSAndrew Thompson 18790f6e8f93SAndrew Thompson /* Create the control endpoint device */ 18802ffd5fdcSHans Petter Selasky udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0, 18812ffd5fdcSHans Petter Selasky FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600); 18820f6e8f93SAndrew Thompson 18830f6e8f93SAndrew Thompson /* Create a link from /dev/ugenX.X to the default endpoint */ 18842ffd5fdcSHans Petter Selasky if (udev->ctrl_dev != NULL) 18852ffd5fdcSHans Petter Selasky make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name); 18860f6e8f93SAndrew Thompson #endif 1887963169b4SHans Petter Selasky /* Initialise device */ 1888963169b4SHans Petter Selasky if (bus->methods->device_init != NULL) { 1889963169b4SHans Petter Selasky err = (bus->methods->device_init) (udev); 1890963169b4SHans Petter Selasky if (err != 0) { 1891963169b4SHans Petter Selasky DPRINTFN(0, "device init %d failed " 1892963169b4SHans Petter Selasky "(%s, ignored)\n", device_index, 1893963169b4SHans Petter Selasky usbd_errstr(err)); 1894963169b4SHans Petter Selasky goto done; 1895963169b4SHans Petter Selasky } 1896963169b4SHans Petter Selasky } 1897963169b4SHans Petter Selasky /* set powered device state after device init is complete */ 1898963169b4SHans Petter Selasky usb_set_device_state(udev, USB_STATE_POWERED); 1899963169b4SHans Petter Selasky 1900f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 190102ac6454SAndrew Thompson 1902a593f6b8SAndrew Thompson err = usbd_req_set_address(udev, NULL, device_index); 190302ac6454SAndrew Thompson 1904963169b4SHans Petter Selasky /* 1905963169b4SHans Petter Selasky * This is the new USB device address from now on, if 1906963169b4SHans Petter Selasky * the set address request didn't set it already. 1907963169b4SHans Petter Selasky */ 1908963169b4SHans Petter Selasky if (udev->address == USB_START_ADDR) 190902ac6454SAndrew Thompson udev->address = device_index; 191002ac6454SAndrew Thompson 191102ac6454SAndrew Thompson /* 191202ac6454SAndrew Thompson * We ignore any set-address errors, hence there are 191302ac6454SAndrew Thompson * buggy USB devices out there that actually receive 191402ac6454SAndrew Thompson * the SETUP PID, but manage to set the address before 191502ac6454SAndrew Thompson * the STATUS stage is ACK'ed. If the device responds 191602ac6454SAndrew Thompson * to the subsequent get-descriptor at the new 191702ac6454SAndrew Thompson * address, then we know that the set-address command 191802ac6454SAndrew Thompson * was successful. 191902ac6454SAndrew Thompson */ 192002ac6454SAndrew Thompson if (err) { 192102ac6454SAndrew Thompson DPRINTFN(0, "set address %d failed " 192203797f33SAndrew Thompson "(%s, ignored)\n", udev->address, 1923a593f6b8SAndrew Thompson usbd_errstr(err)); 192402ac6454SAndrew Thompson } 192502ac6454SAndrew Thompson } else { 192602ac6454SAndrew Thompson /* We are not self powered */ 192702ac6454SAndrew Thompson udev->flags.self_powered = 0; 192802ac6454SAndrew Thompson 192902ac6454SAndrew Thompson /* Set unconfigured state */ 193002ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 193102ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 193202ac6454SAndrew Thompson 193302ac6454SAndrew Thompson /* Setup USB descriptors */ 1934a593f6b8SAndrew Thompson err = (usb_temp_setup_by_index_p) (udev, usb_template); 193502ac6454SAndrew Thompson if (err) { 19366be75992SEdward Tomasz Napierala DPRINTFN(0, "setting up USB template failed - " 19376be75992SEdward Tomasz Napierala "usb_template(4) not loaded?\n"); 193802ac6454SAndrew Thompson goto done; 193902ac6454SAndrew Thompson } 194002ac6454SAndrew Thompson } 1941a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 194202ac6454SAndrew Thompson 1943963169b4SHans Petter Selasky /* setup the device descriptor and the initial "wMaxPacketSize" */ 1944963169b4SHans Petter Selasky err = usbd_setup_device_desc(udev, NULL); 1945963169b4SHans Petter Selasky 1946963169b4SHans Petter Selasky if (err != 0) { 194723de050bSHans Petter Selasky /* try to enumerate two more times */ 1948a593f6b8SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 194923de050bSHans Petter Selasky if (err != 0) { 195023de050bSHans Petter Selasky err = usbd_req_re_enumerate(udev, NULL); 195123de050bSHans Petter Selasky if (err != 0) { 195202ac6454SAndrew Thompson goto done; 195302ac6454SAndrew Thompson } 195423de050bSHans Petter Selasky } 195523de050bSHans Petter Selasky } 195602ac6454SAndrew Thompson 195702ac6454SAndrew Thompson /* 195802ac6454SAndrew Thompson * Setup temporary USB attach args so that we can figure out some 195902ac6454SAndrew Thompson * basic quirks for this device. 196002ac6454SAndrew Thompson */ 1961a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 196202ac6454SAndrew Thompson 1963a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) { 196402ac6454SAndrew Thompson udev->flags.uq_bus_powered = 1; 196502ac6454SAndrew Thompson } 1966a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) { 196702ac6454SAndrew Thompson udev->flags.no_strings = 1; 196802ac6454SAndrew Thompson } 19696950c75fSHans Petter Selasky 1970f54ab96dSHans Petter Selasky usb_get_langid(udev); 19716950c75fSHans Petter Selasky 197202ac6454SAndrew Thompson /* assume 100mA bus powered for now. Changed when configured. */ 197302ac6454SAndrew Thompson udev->power = USB_MIN_POWER; 1974c86aead6SAndrew Thompson /* fetch the vendor and product strings from the device */ 1975f54ab96dSHans Petter Selasky usb_set_device_strings(udev); 197602ac6454SAndrew Thompson 19772a4c6157SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 19782a4c6157SAndrew Thompson /* USB device mode setup is complete */ 19792a4c6157SAndrew Thompson err = 0; 19802a4c6157SAndrew Thompson goto config_done; 19812a4c6157SAndrew Thompson } 198202ac6454SAndrew Thompson 198302ac6454SAndrew Thompson /* 198402ac6454SAndrew Thompson * Most USB devices should attach to config index 0 by 198502ac6454SAndrew Thompson * default 198602ac6454SAndrew Thompson */ 1987a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 198802ac6454SAndrew Thompson config_index = 0; 198902ac6454SAndrew Thompson config_quirk = 1; 1990a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 199102ac6454SAndrew Thompson config_index = 1; 199202ac6454SAndrew Thompson config_quirk = 1; 1993a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 199402ac6454SAndrew Thompson config_index = 2; 199502ac6454SAndrew Thompson config_quirk = 1; 1996a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 199702ac6454SAndrew Thompson config_index = 3; 199802ac6454SAndrew Thompson config_quirk = 1; 1999a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 200002ac6454SAndrew Thompson config_index = 4; 200102ac6454SAndrew Thompson config_quirk = 1; 200202ac6454SAndrew Thompson } else { 200302ac6454SAndrew Thompson config_index = 0; 200402ac6454SAndrew Thompson config_quirk = 0; 200502ac6454SAndrew Thompson } 200602ac6454SAndrew Thompson 20072a4c6157SAndrew Thompson set_config_failed = 0; 200802ac6454SAndrew Thompson repeat_set_config: 200902ac6454SAndrew Thompson 201002ac6454SAndrew Thompson DPRINTF("setting config %u\n", config_index); 201102ac6454SAndrew Thompson 201202ac6454SAndrew Thompson /* get the USB device configured */ 2013a593f6b8SAndrew Thompson err = usbd_set_config_index(udev, config_index); 201402ac6454SAndrew Thompson if (err) { 201502ac6454SAndrew Thompson if (udev->ddesc.bNumConfigurations != 0) { 201602ac6454SAndrew Thompson if (!set_config_failed) { 201702ac6454SAndrew Thompson set_config_failed = 1; 201802ac6454SAndrew Thompson /* XXX try to re-enumerate the device */ 20192a4c6157SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 202002ac6454SAndrew Thompson if (err == 0) 202102ac6454SAndrew Thompson goto repeat_set_config; 202202ac6454SAndrew Thompson } 20232a4c6157SAndrew Thompson DPRINTFN(0, "Failure selecting configuration index %u:" 20242a4c6157SAndrew Thompson "%s, port %u, addr %u (ignored)\n", 2025a593f6b8SAndrew Thompson config_index, usbd_errstr(err), udev->port_no, 202602ac6454SAndrew Thompson udev->address); 202702ac6454SAndrew Thompson } 202802ac6454SAndrew Thompson /* 20292a4c6157SAndrew Thompson * Some USB devices do not have any configurations. Ignore any 20302a4c6157SAndrew Thompson * set config failures! 203102ac6454SAndrew Thompson */ 203202ac6454SAndrew Thompson err = 0; 20332a4c6157SAndrew Thompson goto config_done; 20342a4c6157SAndrew Thompson } 20352a4c6157SAndrew Thompson if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) { 203602ac6454SAndrew Thompson if ((udev->cdesc->bNumInterface < 2) && 20372a4c6157SAndrew Thompson usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) { 20382a4c6157SAndrew Thompson DPRINTFN(0, "Found no endpoints, trying next config\n"); 203902ac6454SAndrew Thompson config_index++; 204002ac6454SAndrew Thompson goto repeat_set_config; 204102ac6454SAndrew Thompson } 204215d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 204302ac6454SAndrew Thompson if (config_index == 0) { 204402ac6454SAndrew Thompson /* 204502ac6454SAndrew Thompson * Try to figure out if we have an 204602ac6454SAndrew Thompson * auto-install disk there: 204702ac6454SAndrew Thompson */ 2048d84a79e7SAndrew Thompson if (usb_iface_is_cdrom(udev, 0)) { 204902ac6454SAndrew Thompson DPRINTFN(0, "Found possible auto-install " 205002ac6454SAndrew Thompson "disk (trying next config)\n"); 205102ac6454SAndrew Thompson config_index++; 205202ac6454SAndrew Thompson goto repeat_set_config; 205302ac6454SAndrew Thompson } 205402ac6454SAndrew Thompson } 205515d4edd3SHans Petter Selasky #endif 20562a4c6157SAndrew Thompson } 205715d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 2058d46dc4adSHans Petter Selasky if (set_config_failed == 0 && config_index == 0 && 20599157ad4bSHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 && 20609157ad4bSHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0) { 2061d46dc4adSHans Petter Selasky 2062d46dc4adSHans Petter Selasky /* 2063d46dc4adSHans Petter Selasky * Try to figure out if there are any MSC quirks we 2064d46dc4adSHans Petter Selasky * should apply automatically: 2065d46dc4adSHans Petter Selasky */ 2066d46dc4adSHans Petter Selasky err = usb_msc_auto_quirk(udev, 0); 2067d46dc4adSHans Petter Selasky 2068d46dc4adSHans Petter Selasky if (err != 0) { 2069d46dc4adSHans Petter Selasky set_config_failed = 1; 2070d46dc4adSHans Petter Selasky goto repeat_set_config; 2071d46dc4adSHans Petter Selasky } 2072d46dc4adSHans Petter Selasky } 207315d4edd3SHans Petter Selasky #endif 207402ac6454SAndrew Thompson 20752a4c6157SAndrew Thompson config_done: 207602ac6454SAndrew Thompson DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 207702ac6454SAndrew Thompson udev->address, udev, udev->parent_hub); 207802ac6454SAndrew Thompson 207902ac6454SAndrew Thompson /* register our device - we are ready */ 2080a593f6b8SAndrew Thompson usb_bus_port_set_device(bus, parent_hub ? 208102ac6454SAndrew Thompson parent_hub->hub->ports + port_index : NULL, udev, device_index); 208202ac6454SAndrew Thompson 20830f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 20848755859aSAndrew Thompson /* Symlink the ugen device name */ 2085a593f6b8SAndrew Thompson udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); 20868755859aSAndrew Thompson 20878755859aSAndrew Thompson /* Announce device */ 208888f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s\n", udev->ugen_name, 208988f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 209002ac6454SAndrew Thompson device_get_nameunit(udev->bus->bdev)); 20918bb77249SHans Petter Selasky #endif 20927efaaa9aSAndrew Thompson 20938bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 20943acd904dSAndrew Thompson usb_notify_addq("ATTACH", udev); 20958755859aSAndrew Thompson #endif 209602ac6454SAndrew Thompson done: 209702ac6454SAndrew Thompson if (err) { 2098d88688c7SAndrew Thompson /* 2099d88688c7SAndrew Thompson * Free USB device and all subdevices, if any. 2100d88688c7SAndrew Thompson */ 2101d88688c7SAndrew Thompson usb_free_device(udev, 0); 210202ac6454SAndrew Thompson udev = NULL; 210302ac6454SAndrew Thompson } 210402ac6454SAndrew Thompson return (udev); 210502ac6454SAndrew Thompson } 210602ac6454SAndrew Thompson 21070f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 21082ffd5fdcSHans Petter Selasky struct usb_fs_privdata * 21092ffd5fdcSHans Petter Selasky usb_make_dev(struct usb_device *udev, const char *devname, int ep, 21102ffd5fdcSHans Petter Selasky int fi, int rwmode, uid_t uid, gid_t gid, int mode) 2111ee3e3ff5SAndrew Thompson { 2112760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2113ac3490fdSHans Petter Selasky struct make_dev_args args; 21142ffd5fdcSHans Petter Selasky char buffer[32]; 2115ee3e3ff5SAndrew Thompson 2116ee3e3ff5SAndrew Thompson /* Store information to locate ourselves again later */ 2117760bc48eSAndrew Thompson pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, 2118ee3e3ff5SAndrew Thompson M_WAITOK | M_ZERO); 2119ee3e3ff5SAndrew Thompson pd->bus_index = device_get_unit(udev->bus->bdev); 2120ee3e3ff5SAndrew Thompson pd->dev_index = udev->device_index; 2121ee3e3ff5SAndrew Thompson pd->ep_addr = ep; 21222ffd5fdcSHans Petter Selasky pd->fifo_index = fi; 21232ffd5fdcSHans Petter Selasky pd->mode = rwmode; 2124ee3e3ff5SAndrew Thompson 2125ee3e3ff5SAndrew Thompson /* Now, create the device itself */ 21262ffd5fdcSHans Petter Selasky if (devname == NULL) { 21272ffd5fdcSHans Petter Selasky devname = buffer; 21282ffd5fdcSHans Petter Selasky snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u", 2129f35aaff0SAndrew Thompson pd->bus_index, pd->dev_index, pd->ep_addr); 21302ffd5fdcSHans Petter Selasky } 21312ffd5fdcSHans Petter Selasky 2132ac3490fdSHans Petter Selasky /* Setup arguments for make_dev_s() */ 2133ac3490fdSHans Petter Selasky make_dev_args_init(&args); 2134ac3490fdSHans Petter Selasky args.mda_devsw = &usb_devsw; 2135ac3490fdSHans Petter Selasky args.mda_uid = uid; 2136ac3490fdSHans Petter Selasky args.mda_gid = gid; 2137ac3490fdSHans Petter Selasky args.mda_mode = mode; 2138ac3490fdSHans Petter Selasky args.mda_si_drv1 = pd; 21392ffd5fdcSHans Petter Selasky 2140ac3490fdSHans Petter Selasky if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) { 21412ffd5fdcSHans Petter Selasky DPRINTFN(0, "Failed to create device %s\n", devname); 21422ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 21432ffd5fdcSHans Petter Selasky return (NULL); 21442ffd5fdcSHans Petter Selasky } 21452ffd5fdcSHans Petter Selasky return (pd); 21462ffd5fdcSHans Petter Selasky } 21472ffd5fdcSHans Petter Selasky 21482ffd5fdcSHans Petter Selasky void 2149b78e84d1SHans Petter Selasky usb_destroy_dev_sync(struct usb_fs_privdata *pd) 21502ffd5fdcSHans Petter Selasky { 2151b78e84d1SHans Petter Selasky DPRINTFN(1, "Destroying device at ugen%d.%d\n", 2152b78e84d1SHans Petter Selasky pd->bus_index, pd->dev_index); 21532ffd5fdcSHans Petter Selasky 2154b78e84d1SHans Petter Selasky /* 2155b78e84d1SHans Petter Selasky * Destroy character device synchronously. After this 2156b78e84d1SHans Petter Selasky * all system calls are returned. Can block. 2157b78e84d1SHans Petter Selasky */ 21582ffd5fdcSHans Petter Selasky destroy_dev(pd->cdev); 21592ffd5fdcSHans Petter Selasky 21602ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 2161ee3e3ff5SAndrew Thompson } 2162ee3e3ff5SAndrew Thompson 2163b78e84d1SHans Petter Selasky void 2164b78e84d1SHans Petter Selasky usb_destroy_dev(struct usb_fs_privdata *pd) 2165b78e84d1SHans Petter Selasky { 2166b78e84d1SHans Petter Selasky struct usb_bus *bus; 2167b78e84d1SHans Petter Selasky 2168b78e84d1SHans Petter Selasky if (pd == NULL) 2169b78e84d1SHans Petter Selasky return; 2170b78e84d1SHans Petter Selasky 2171b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 2172b78e84d1SHans Petter Selasky bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index); 2173b78e84d1SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2174b78e84d1SHans Petter Selasky 2175b78e84d1SHans Petter Selasky if (bus == NULL) { 2176b78e84d1SHans Petter Selasky usb_destroy_dev_sync(pd); 2177b78e84d1SHans Petter Selasky return; 2178b78e84d1SHans Petter Selasky } 2179b78e84d1SHans Petter Selasky 218007dbde67SHans Petter Selasky /* make sure we can re-use the device name */ 218107dbde67SHans Petter Selasky delist_dev(pd->cdev); 218207dbde67SHans Petter Selasky 2183b78e84d1SHans Petter Selasky USB_BUS_LOCK(bus); 2184b78e84d1SHans Petter Selasky LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next); 2185b78e84d1SHans Petter Selasky /* get cleanup going */ 2186b78e84d1SHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2187b78e84d1SHans Petter Selasky &bus->cleanup_msg[0], &bus->cleanup_msg[1]); 2188b78e84d1SHans Petter Selasky USB_BUS_UNLOCK(bus); 2189b78e84d1SHans Petter Selasky } 2190b78e84d1SHans Petter Selasky 2191ee3e3ff5SAndrew Thompson static void 2192a593f6b8SAndrew Thompson usb_cdev_create(struct usb_device *udev) 2193ee3e3ff5SAndrew Thompson { 2194760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2195760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 2196760bc48eSAndrew Thompson struct usb_descriptor *desc; 2197760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2198f5f145baSAndrew Thompson int inmode, outmode, inmask, outmask, mode; 2199f5f145baSAndrew Thompson uint8_t ep; 2200ee3e3ff5SAndrew Thompson 2201ee3e3ff5SAndrew Thompson KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 2202ee3e3ff5SAndrew Thompson 2203ee3e3ff5SAndrew Thompson DPRINTFN(2, "Creating device nodes\n"); 2204ee3e3ff5SAndrew Thompson 2205a593f6b8SAndrew Thompson if (usbd_get_mode(udev) == USB_MODE_DEVICE) { 2206ee3e3ff5SAndrew Thompson inmode = FWRITE; 2207ee3e3ff5SAndrew Thompson outmode = FREAD; 2208ee3e3ff5SAndrew Thompson } else { /* USB_MODE_HOST */ 2209ee3e3ff5SAndrew Thompson inmode = FREAD; 2210ee3e3ff5SAndrew Thompson outmode = FWRITE; 2211ee3e3ff5SAndrew Thompson } 2212ee3e3ff5SAndrew Thompson 2213f5f145baSAndrew Thompson inmask = 0; 2214f5f145baSAndrew Thompson outmask = 0; 2215f5f145baSAndrew Thompson desc = NULL; 2216f5f145baSAndrew Thompson 2217f5f145baSAndrew Thompson /* 2218f5f145baSAndrew Thompson * Collect all used endpoint numbers instead of just 2219f5f145baSAndrew Thompson * generating 16 static endpoints. 2220f5f145baSAndrew Thompson */ 2221a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 2222a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 2223f5f145baSAndrew Thompson /* filter out all endpoint descriptors */ 2224f5f145baSAndrew Thompson if ((desc->bDescriptorType == UDESC_ENDPOINT) && 2225f5f145baSAndrew Thompson (desc->bLength >= sizeof(*ed))) { 2226760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)desc; 2227f5f145baSAndrew Thompson 2228f5f145baSAndrew Thompson /* update masks */ 2229f5f145baSAndrew Thompson ep = ed->bEndpointAddress; 2230f5f145baSAndrew Thompson if (UE_GET_DIR(ep) == UE_DIR_OUT) 2231f5f145baSAndrew Thompson outmask |= 1 << UE_GET_ADDR(ep); 2232f5f145baSAndrew Thompson else 2233f5f145baSAndrew Thompson inmask |= 1 << UE_GET_ADDR(ep); 2234f5f145baSAndrew Thompson } 2235f5f145baSAndrew Thompson } 2236ee3e3ff5SAndrew Thompson 2237ee3e3ff5SAndrew Thompson /* Create all available endpoints except EP0 */ 2238ee3e3ff5SAndrew Thompson for (ep = 1; ep < 16; ep++) { 22392ffd5fdcSHans Petter Selasky mode = (inmask & (1 << ep)) ? inmode : 0; 22402ffd5fdcSHans Petter Selasky mode |= (outmask & (1 << ep)) ? outmode : 0; 2241ee3e3ff5SAndrew Thompson if (mode == 0) 2242ee3e3ff5SAndrew Thompson continue; /* no IN or OUT endpoint */ 2243ee3e3ff5SAndrew Thompson 22442ffd5fdcSHans Petter Selasky pd = usb_make_dev(udev, NULL, ep, 0, 22452ffd5fdcSHans Petter Selasky mode, UID_ROOT, GID_OPERATOR, 0600); 22462ffd5fdcSHans Petter Selasky 22472ffd5fdcSHans Petter Selasky if (pd != NULL) 2248ee3e3ff5SAndrew Thompson LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 2249ee3e3ff5SAndrew Thompson } 2250ee3e3ff5SAndrew Thompson } 2251ee3e3ff5SAndrew Thompson 2252ee3e3ff5SAndrew Thompson static void 2253a593f6b8SAndrew Thompson usb_cdev_free(struct usb_device *udev) 2254ee3e3ff5SAndrew Thompson { 2255760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2256ee3e3ff5SAndrew Thompson 2257ee3e3ff5SAndrew Thompson DPRINTFN(2, "Freeing device nodes\n"); 2258ee3e3ff5SAndrew Thompson 2259ee3e3ff5SAndrew Thompson while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 2260ee3e3ff5SAndrew Thompson KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 2261ee3e3ff5SAndrew Thompson 2262ee3e3ff5SAndrew Thompson LIST_REMOVE(pd, pd_next); 2263ee3e3ff5SAndrew Thompson 22642ffd5fdcSHans Petter Selasky usb_destroy_dev(pd); 22652ffd5fdcSHans Petter Selasky } 2266ee3e3ff5SAndrew Thompson } 22670f6e8f93SAndrew Thompson #endif 2268ee3e3ff5SAndrew Thompson 226902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2270a593f6b8SAndrew Thompson * usb_free_device 227102ac6454SAndrew Thompson * 2272d88688c7SAndrew Thompson * This function is NULL safe and will free an USB device and its 2273d88688c7SAndrew Thompson * children devices, if any. 2274bdd41206SAndrew Thompson * 2275d88688c7SAndrew Thompson * Flag values: Reserved, set to zero. 227602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 227702ac6454SAndrew Thompson void 2278a593f6b8SAndrew Thompson usb_free_device(struct usb_device *udev, uint8_t flag) 227902ac6454SAndrew Thompson { 2280760bc48eSAndrew Thompson struct usb_bus *bus; 2281bdd41206SAndrew Thompson 2282bdd41206SAndrew Thompson if (udev == NULL) 2283bdd41206SAndrew Thompson return; /* already freed */ 228402ac6454SAndrew Thompson 228502ac6454SAndrew Thompson DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 228602ac6454SAndrew Thompson 2287bd216778SAndrew Thompson bus = udev->bus; 2288f97da128SHans Petter Selasky 2289f97da128SHans Petter Selasky /* set DETACHED state to prevent any further references */ 2290a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_DETACHED); 2291bdd41206SAndrew Thompson 22928bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 22933acd904dSAndrew Thompson usb_notify_addq("DETACH", udev); 22948bb77249SHans Petter Selasky #endif 229502ac6454SAndrew Thompson 22968bb77249SHans Petter Selasky #if USB_HAVE_UGEN 2297cabe79d9SMarius Strobl if (!rebooting) { 229888f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name, 229988f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 230088f9a4ffSEdward Tomasz Napierala device_get_nameunit(bus->bdev)); 2301cabe79d9SMarius Strobl } 230202ac6454SAndrew Thompson 2303ee3e3ff5SAndrew Thompson /* Destroy UGEN symlink, if any */ 230402ac6454SAndrew Thompson if (udev->ugen_symlink) { 2305a593f6b8SAndrew Thompson usb_free_symlink(udev->ugen_symlink); 230602ac6454SAndrew Thompson udev->ugen_symlink = NULL; 230702ac6454SAndrew Thompson } 230802ac6454SAndrew Thompson 23092ffd5fdcSHans Petter Selasky usb_destroy_dev(udev->ctrl_dev); 23100f6e8f93SAndrew Thompson #endif 231102ac6454SAndrew Thompson 2312f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 231302ac6454SAndrew Thompson /* stop receiving any control transfers (Device Side Mode) */ 23145b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 231502ac6454SAndrew Thompson } 231602ac6454SAndrew Thompson 2317bdd41206SAndrew Thompson /* the following will get the device unconfigured in software */ 2318d88688c7SAndrew Thompson usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0); 231902ac6454SAndrew Thompson 2320f97da128SHans Petter Selasky /* final device unregister after all character devices are closed */ 2321f97da128SHans Petter Selasky usb_bus_port_set_device(bus, udev->parent_hub ? 2322f97da128SHans Petter Selasky udev->parent_hub->hub->ports + udev->port_index : NULL, 2323f97da128SHans Petter Selasky NULL, USB_ROOT_HUB_ADDR); 2324f97da128SHans Petter Selasky 232502ac6454SAndrew Thompson /* unsetup any leftover default USB transfers */ 23265b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 232702ac6454SAndrew Thompson 232802ac6454SAndrew Thompson /* template unsetup, if any */ 2329a593f6b8SAndrew Thompson (usb_temp_unsetup_p) (udev); 233002ac6454SAndrew Thompson 233102ac6454SAndrew Thompson /* 233202ac6454SAndrew Thompson * Make sure that our clear-stall messages are not queued 233302ac6454SAndrew Thompson * anywhere: 233402ac6454SAndrew Thompson */ 233502ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 233643ea03d7SHans Petter Selasky usb_proc_mwait(USB_BUS_CS_PROC(udev->bus), 233702ac6454SAndrew Thompson &udev->cs_msg[0], &udev->cs_msg[1]); 233802ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 233902ac6454SAndrew Thompson 2340b78e84d1SHans Petter Selasky /* wait for all references to go away */ 2341b78e84d1SHans Petter Selasky usb_wait_pending_refs(udev); 2342b78e84d1SHans Petter Selasky 234391cd9240SAndrew Thompson sx_destroy(&udev->enum_sx); 23442df1e9a6SAndrew Thompson sx_destroy(&udev->sr_sx); 234564cb5e2aSHans Petter Selasky sx_destroy(&udev->ctrl_sx); 234602ac6454SAndrew Thompson 234791cd9240SAndrew Thompson cv_destroy(&udev->ctrlreq_cv); 234891cd9240SAndrew Thompson cv_destroy(&udev->ref_cv); 234902ac6454SAndrew Thompson 235091cd9240SAndrew Thompson mtx_destroy(&udev->device_mtx); 23518755859aSAndrew Thompson #if USB_HAVE_UGEN 2352ee3e3ff5SAndrew Thompson KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 23538755859aSAndrew Thompson #endif 235402ac6454SAndrew Thompson 2355963169b4SHans Petter Selasky /* Uninitialise device */ 2356963169b4SHans Petter Selasky if (bus->methods->device_uninit != NULL) 2357963169b4SHans Petter Selasky (bus->methods->device_uninit) (udev); 2358963169b4SHans Petter Selasky 235902ac6454SAndrew Thompson /* free device */ 236028212de2SAndrew Thompson free(udev->serial, M_USB); 236128212de2SAndrew Thompson free(udev->manufacturer, M_USB); 236228212de2SAndrew Thompson free(udev->product, M_USB); 236302ac6454SAndrew Thompson free(udev, M_USB); 236402ac6454SAndrew Thompson } 236502ac6454SAndrew Thompson 236602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2367a593f6b8SAndrew Thompson * usbd_get_iface 236802ac6454SAndrew Thompson * 236902ac6454SAndrew Thompson * This function is the safe way to get the USB interface structure 237002ac6454SAndrew Thompson * pointer by interface index. 237102ac6454SAndrew Thompson * 237202ac6454SAndrew Thompson * Return values: 237302ac6454SAndrew Thompson * NULL: Interface not present. 237402ac6454SAndrew Thompson * Else: Pointer to USB interface structure. 237502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 2376760bc48eSAndrew Thompson struct usb_interface * 2377a593f6b8SAndrew Thompson usbd_get_iface(struct usb_device *udev, uint8_t iface_index) 237802ac6454SAndrew Thompson { 2379760bc48eSAndrew Thompson struct usb_interface *iface = udev->ifaces + iface_index; 238002ac6454SAndrew Thompson 2381bdd41206SAndrew Thompson if (iface_index >= udev->ifaces_max) 238202ac6454SAndrew Thompson return (NULL); 238302ac6454SAndrew Thompson return (iface); 238402ac6454SAndrew Thompson } 238502ac6454SAndrew Thompson 238602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 23873c8d24f4SAndrew Thompson * usbd_find_descriptor 238802ac6454SAndrew Thompson * 238902ac6454SAndrew Thompson * This function will lookup the first descriptor that matches the 239002ac6454SAndrew Thompson * criteria given by the arguments "type" and "subtype". Descriptors 239102ac6454SAndrew Thompson * will only be searched within the interface having the index 239202ac6454SAndrew Thompson * "iface_index". If the "id" argument points to an USB descriptor, 239302ac6454SAndrew Thompson * it will be skipped before the search is started. This allows 239402ac6454SAndrew Thompson * searching for multiple descriptors using the same criteria. Else 239502ac6454SAndrew Thompson * the search is started after the interface descriptor. 239602ac6454SAndrew Thompson * 239702ac6454SAndrew Thompson * Return values: 239802ac6454SAndrew Thompson * NULL: End of descriptors 239902ac6454SAndrew Thompson * Else: A descriptor matching the criteria 240002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 240102ac6454SAndrew Thompson void * 24023c8d24f4SAndrew Thompson usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index, 240302ac6454SAndrew Thompson uint8_t type, uint8_t type_mask, 240402ac6454SAndrew Thompson uint8_t subtype, uint8_t subtype_mask) 240502ac6454SAndrew Thompson { 2406760bc48eSAndrew Thompson struct usb_descriptor *desc; 2407760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2408760bc48eSAndrew Thompson struct usb_interface *iface; 240902ac6454SAndrew Thompson 2410a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 241102ac6454SAndrew Thompson if (cd == NULL) { 241202ac6454SAndrew Thompson return (NULL); 241302ac6454SAndrew Thompson } 241402ac6454SAndrew Thompson if (id == NULL) { 2415a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, iface_index); 241602ac6454SAndrew Thompson if (iface == NULL) { 241702ac6454SAndrew Thompson return (NULL); 241802ac6454SAndrew Thompson } 2419a593f6b8SAndrew Thompson id = usbd_get_interface_descriptor(iface); 242002ac6454SAndrew Thompson if (id == NULL) { 242102ac6454SAndrew Thompson return (NULL); 242202ac6454SAndrew Thompson } 242302ac6454SAndrew Thompson } 242402ac6454SAndrew Thompson desc = (void *)id; 242502ac6454SAndrew Thompson 2426a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 242702ac6454SAndrew Thompson 242802ac6454SAndrew Thompson if (desc->bDescriptorType == UDESC_INTERFACE) { 242902ac6454SAndrew Thompson break; 243002ac6454SAndrew Thompson } 243102ac6454SAndrew Thompson if (((desc->bDescriptorType & type_mask) == type) && 243202ac6454SAndrew Thompson ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 243302ac6454SAndrew Thompson return (desc); 243402ac6454SAndrew Thompson } 243502ac6454SAndrew Thompson } 243602ac6454SAndrew Thompson return (NULL); 243702ac6454SAndrew Thompson } 243802ac6454SAndrew Thompson 243902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2440a593f6b8SAndrew Thompson * usb_devinfo 244102ac6454SAndrew Thompson * 244202ac6454SAndrew Thompson * This function will dump information from the device descriptor 244302ac6454SAndrew Thompson * belonging to the USB device pointed to by "udev", to the string 244402ac6454SAndrew Thompson * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 244502ac6454SAndrew Thompson * including the terminating zero. 244602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 244702ac6454SAndrew Thompson void 2448a593f6b8SAndrew Thompson usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) 244902ac6454SAndrew Thompson { 2450760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 245102ac6454SAndrew Thompson uint16_t bcdDevice; 245202ac6454SAndrew Thompson uint16_t bcdUSB; 245302ac6454SAndrew Thompson 245402ac6454SAndrew Thompson bcdUSB = UGETW(udd->bcdUSB); 245502ac6454SAndrew Thompson bcdDevice = UGETW(udd->bcdDevice); 245602ac6454SAndrew Thompson 245702ac6454SAndrew Thompson if (udd->bDeviceClass != 0xFF) { 245802ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 24598755859aSAndrew Thompson "%x.%02x, addr %d", 2460ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2461ae538d85SAndrew Thompson usb_get_product(udev), 246202ac6454SAndrew Thompson udd->bDeviceClass, udd->bDeviceSubClass, 246302ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 246402ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 246502ac6454SAndrew Thompson udev->address); 246602ac6454SAndrew Thompson } else { 246702ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 24688755859aSAndrew Thompson "%x.%02x, addr %d", 2469ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2470ae538d85SAndrew Thompson usb_get_product(udev), 247102ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 247202ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 247302ac6454SAndrew Thompson udev->address); 247402ac6454SAndrew Thompson } 247502ac6454SAndrew Thompson } 247602ac6454SAndrew Thompson 2477ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 247802ac6454SAndrew Thompson /* 247902ac6454SAndrew Thompson * Descriptions of of known vendors and devices ("products"). 248002ac6454SAndrew Thompson */ 248102ac6454SAndrew Thompson struct usb_knowndev { 248202ac6454SAndrew Thompson uint16_t vendor; 248302ac6454SAndrew Thompson uint16_t product; 248402ac6454SAndrew Thompson uint32_t flags; 248502ac6454SAndrew Thompson const char *vendorname; 248602ac6454SAndrew Thompson const char *productname; 248702ac6454SAndrew Thompson }; 248802ac6454SAndrew Thompson 248902ac6454SAndrew Thompson #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 249002ac6454SAndrew Thompson 249102ac6454SAndrew Thompson #include "usbdevs.h" 249202ac6454SAndrew Thompson #include "usbdevs_data.h" 249302ac6454SAndrew Thompson #endif /* USB_VERBOSE */ 249402ac6454SAndrew Thompson 2495f54ab96dSHans Petter Selasky void 2496f54ab96dSHans Petter Selasky usb_set_device_strings(struct usb_device *udev) 249702ac6454SAndrew Thompson { 2498760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 2499ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 250002ac6454SAndrew Thompson const struct usb_knowndev *kdp; 250102ac6454SAndrew Thompson #endif 25022df1e9a6SAndrew Thompson char *temp_ptr; 25037a918edfSAndrew Thompson size_t temp_size; 250402ac6454SAndrew Thompson uint16_t vendor_id; 250502ac6454SAndrew Thompson uint16_t product_id; 25066950c75fSHans Petter Selasky uint8_t do_unlock; 250702ac6454SAndrew Thompson 25086950c75fSHans Petter Selasky /* Protect scratch area */ 250964cb5e2aSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 25106950c75fSHans Petter Selasky 25116950c75fSHans Petter Selasky temp_ptr = (char *)udev->scratch.data; 25126950c75fSHans Petter Selasky temp_size = sizeof(udev->scratch.data); 25137a918edfSAndrew Thompson 251402ac6454SAndrew Thompson vendor_id = UGETW(udd->idVendor); 251502ac6454SAndrew Thompson product_id = UGETW(udd->idProduct); 251602ac6454SAndrew Thompson 2517f54ab96dSHans Petter Selasky /* cleanup old strings, if any */ 2518f54ab96dSHans Petter Selasky free(udev->serial, M_USB); 2519f54ab96dSHans Petter Selasky free(udev->manufacturer, M_USB); 2520f54ab96dSHans Petter Selasky free(udev->product, M_USB); 2521f54ab96dSHans Petter Selasky 2522f54ab96dSHans Petter Selasky /* zero the string pointers */ 2523f54ab96dSHans Petter Selasky udev->serial = NULL; 2524f54ab96dSHans Petter Selasky udev->manufacturer = NULL; 2525f54ab96dSHans Petter Selasky udev->product = NULL; 2526f54ab96dSHans Petter Selasky 2527c86aead6SAndrew Thompson /* get serial number string */ 25287a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2529c86aead6SAndrew Thompson udev->ddesc.iSerialNumber); 25307a918edfSAndrew Thompson udev->serial = strdup(temp_ptr, M_USB); 253102ac6454SAndrew Thompson 2532c86aead6SAndrew Thompson /* get manufacturer string */ 25337a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2534c86aead6SAndrew Thompson udev->ddesc.iManufacturer); 25357a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 25367a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 25377a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 2538c86aead6SAndrew Thompson 2539c86aead6SAndrew Thompson /* get product string */ 25407a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2541c86aead6SAndrew Thompson udev->ddesc.iProduct); 25427a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 25437a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 25447a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 2545c86aead6SAndrew Thompson 2546ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 2547c86aead6SAndrew Thompson if (udev->manufacturer == NULL || udev->product == NULL) { 2548c86aead6SAndrew Thompson for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) { 254902ac6454SAndrew Thompson if (kdp->vendor == vendor_id && 255002ac6454SAndrew Thompson (kdp->product == product_id || 255102ac6454SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 255202ac6454SAndrew Thompson break; 255302ac6454SAndrew Thompson } 255402ac6454SAndrew Thompson if (kdp->vendorname != NULL) { 2555c86aead6SAndrew Thompson /* XXX should use pointer to knowndevs string */ 2556c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 2557c86aead6SAndrew Thompson udev->manufacturer = strdup(kdp->vendorname, 2558c86aead6SAndrew Thompson M_USB); 2559c86aead6SAndrew Thompson } 2560c86aead6SAndrew Thompson if (udev->product == NULL && 2561c86aead6SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) { 2562c86aead6SAndrew Thompson udev->product = strdup(kdp->productname, 2563c86aead6SAndrew Thompson M_USB); 2564c86aead6SAndrew Thompson } 256502ac6454SAndrew Thompson } 256602ac6454SAndrew Thompson } 256702ac6454SAndrew Thompson #endif 2568c86aead6SAndrew Thompson /* Provide default strings if none were found */ 2569c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 25707a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id); 25717a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 257202ac6454SAndrew Thompson } 2573c86aead6SAndrew Thompson if (udev->product == NULL) { 25747a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "product 0x%04x", product_id); 25757a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 257602ac6454SAndrew Thompson } 25776950c75fSHans Petter Selasky 25786950c75fSHans Petter Selasky if (do_unlock) 257964cb5e2aSHans Petter Selasky usbd_ctrl_unlock(udev); 258002ac6454SAndrew Thompson } 258102ac6454SAndrew Thompson 258202ac6454SAndrew Thompson /* 258302ac6454SAndrew Thompson * Returns: 258402ac6454SAndrew Thompson * See: USB_MODE_XXX 258502ac6454SAndrew Thompson */ 2586f29a0724SAndrew Thompson enum usb_hc_mode 2587a593f6b8SAndrew Thompson usbd_get_mode(struct usb_device *udev) 258802ac6454SAndrew Thompson { 2589f29a0724SAndrew Thompson return (udev->flags.usb_mode); 259002ac6454SAndrew Thompson } 259102ac6454SAndrew Thompson 259202ac6454SAndrew Thompson /* 259302ac6454SAndrew Thompson * Returns: 259402ac6454SAndrew Thompson * See: USB_SPEED_XXX 259502ac6454SAndrew Thompson */ 25968d2dd5ddSAndrew Thompson enum usb_dev_speed 2597a593f6b8SAndrew Thompson usbd_get_speed(struct usb_device *udev) 259802ac6454SAndrew Thompson { 259902ac6454SAndrew Thompson return (udev->speed); 260002ac6454SAndrew Thompson } 260102ac6454SAndrew Thompson 260202ac6454SAndrew Thompson uint32_t 2603a593f6b8SAndrew Thompson usbd_get_isoc_fps(struct usb_device *udev) 260402ac6454SAndrew Thompson { 260502ac6454SAndrew Thompson ; /* indent fix */ 260602ac6454SAndrew Thompson switch (udev->speed) { 260702ac6454SAndrew Thompson case USB_SPEED_LOW: 260802ac6454SAndrew Thompson case USB_SPEED_FULL: 260902ac6454SAndrew Thompson return (1000); 261002ac6454SAndrew Thompson default: 261102ac6454SAndrew Thompson return (8000); 261202ac6454SAndrew Thompson } 261302ac6454SAndrew Thompson } 261402ac6454SAndrew Thompson 2615760bc48eSAndrew Thompson struct usb_device_descriptor * 2616a593f6b8SAndrew Thompson usbd_get_device_descriptor(struct usb_device *udev) 261702ac6454SAndrew Thompson { 261802ac6454SAndrew Thompson if (udev == NULL) 261902ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 262002ac6454SAndrew Thompson return (&udev->ddesc); 262102ac6454SAndrew Thompson } 262202ac6454SAndrew Thompson 2623760bc48eSAndrew Thompson struct usb_config_descriptor * 2624a593f6b8SAndrew Thompson usbd_get_config_descriptor(struct usb_device *udev) 262502ac6454SAndrew Thompson { 262602ac6454SAndrew Thompson if (udev == NULL) 262702ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 262802ac6454SAndrew Thompson return (udev->cdesc); 262902ac6454SAndrew Thompson } 263002ac6454SAndrew Thompson 263102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2632a593f6b8SAndrew Thompson * usb_test_quirk - test a device for a given quirk 263302ac6454SAndrew Thompson * 263402ac6454SAndrew Thompson * Return values: 263502ac6454SAndrew Thompson * 0: The USB device does not have the given quirk. 263602ac6454SAndrew Thompson * Else: The USB device has the given quirk. 263702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 263802ac6454SAndrew Thompson uint8_t 2639a593f6b8SAndrew Thompson usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk) 264002ac6454SAndrew Thompson { 264102ac6454SAndrew Thompson uint8_t found; 26423f37fb62SHans Petter Selasky uint8_t x; 26433f37fb62SHans Petter Selasky 26443f37fb62SHans Petter Selasky if (quirk == UQ_NONE) 26453f37fb62SHans Petter Selasky return (0); 26463f37fb62SHans Petter Selasky 26473f37fb62SHans Petter Selasky /* search the automatic per device quirks first */ 26483f37fb62SHans Petter Selasky 26493f37fb62SHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 26503f37fb62SHans Petter Selasky if (uaa->device->autoQuirk[x] == quirk) 26513f37fb62SHans Petter Selasky return (1); 26523f37fb62SHans Petter Selasky } 26533f37fb62SHans Petter Selasky 26543f37fb62SHans Petter Selasky /* search global quirk table, if any */ 265502ac6454SAndrew Thompson 2656a593f6b8SAndrew Thompson found = (usb_test_quirk_p) (&uaa->info, quirk); 26573f37fb62SHans Petter Selasky 265802ac6454SAndrew Thompson return (found); 265902ac6454SAndrew Thompson } 266002ac6454SAndrew Thompson 2661760bc48eSAndrew Thompson struct usb_interface_descriptor * 2662a593f6b8SAndrew Thompson usbd_get_interface_descriptor(struct usb_interface *iface) 266302ac6454SAndrew Thompson { 266402ac6454SAndrew Thompson if (iface == NULL) 266502ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 266602ac6454SAndrew Thompson return (iface->idesc); 266702ac6454SAndrew Thompson } 266802ac6454SAndrew Thompson 266902ac6454SAndrew Thompson uint8_t 2670a593f6b8SAndrew Thompson usbd_get_interface_altindex(struct usb_interface *iface) 267102ac6454SAndrew Thompson { 267202ac6454SAndrew Thompson return (iface->alt_index); 267302ac6454SAndrew Thompson } 267402ac6454SAndrew Thompson 267502ac6454SAndrew Thompson uint8_t 2676a593f6b8SAndrew Thompson usbd_get_bus_index(struct usb_device *udev) 267702ac6454SAndrew Thompson { 267802ac6454SAndrew Thompson return ((uint8_t)device_get_unit(udev->bus->bdev)); 267902ac6454SAndrew Thompson } 268002ac6454SAndrew Thompson 268102ac6454SAndrew Thompson uint8_t 2682a593f6b8SAndrew Thompson usbd_get_device_index(struct usb_device *udev) 268302ac6454SAndrew Thompson { 268402ac6454SAndrew Thompson return (udev->device_index); 268502ac6454SAndrew Thompson } 268602ac6454SAndrew Thompson 26878bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 26883acd904dSAndrew Thompson static void 26893acd904dSAndrew Thompson usb_notify_addq(const char *type, struct usb_device *udev) 26903acd904dSAndrew Thompson { 26913acd904dSAndrew Thompson struct usb_interface *iface; 26923acd904dSAndrew Thompson struct sbuf *sb; 26933acd904dSAndrew Thompson int i; 26943acd904dSAndrew Thompson 26953acd904dSAndrew Thompson /* announce the device */ 26963acd904dSAndrew Thompson sb = sbuf_new_auto(); 26973acd904dSAndrew Thompson sbuf_printf(sb, 26988bb77249SHans Petter Selasky #if USB_HAVE_UGEN 26998bb77249SHans Petter Selasky "ugen=%s " 270002c40241SHans Petter Selasky "cdev=%s " 27018bb77249SHans Petter Selasky #endif 27023acd904dSAndrew Thompson "vendor=0x%04x " 27033acd904dSAndrew Thompson "product=0x%04x " 27043acd904dSAndrew Thompson "devclass=0x%02x " 27053acd904dSAndrew Thompson "devsubclass=0x%02x " 27063acd904dSAndrew Thompson "sernum=\"%s\" " 27073acd904dSAndrew Thompson "release=0x%04x " 270866390216SAndrew Thompson "mode=%s " 27093acd904dSAndrew Thompson "port=%u " 27108bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27116df3eebbSNick Hibma "parent=%s" 27128bb77249SHans Petter Selasky #endif 27138bb77249SHans Petter Selasky "", 27148bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27153acd904dSAndrew Thompson udev->ugen_name, 271602c40241SHans Petter Selasky udev->ugen_name, 27178bb77249SHans Petter Selasky #endif 27183acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 27193acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 27203acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 27213acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2722ae538d85SAndrew Thompson usb_get_serial(udev), 27233acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 272466390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 27258bb77249SHans Petter Selasky udev->port_no 27268bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27278bb77249SHans Petter Selasky , udev->parent_hub != NULL ? 27283acd904dSAndrew Thompson udev->parent_hub->ugen_name : 27298bb77249SHans Petter Selasky device_get_nameunit(device_get_parent(udev->bus->bdev)) 27308bb77249SHans Petter Selasky #endif 27318bb77249SHans Petter Selasky ); 27323acd904dSAndrew Thompson sbuf_finish(sb); 27333acd904dSAndrew Thompson devctl_notify("USB", "DEVICE", type, sbuf_data(sb)); 27343acd904dSAndrew Thompson sbuf_delete(sb); 27353acd904dSAndrew Thompson 27363acd904dSAndrew Thompson /* announce each interface */ 27373acd904dSAndrew Thompson for (i = 0; i < USB_IFACE_MAX; i++) { 27383acd904dSAndrew Thompson iface = usbd_get_iface(udev, i); 27393acd904dSAndrew Thompson if (iface == NULL) 27403acd904dSAndrew Thompson break; /* end of interfaces */ 27413acd904dSAndrew Thompson if (iface->idesc == NULL) 27423acd904dSAndrew Thompson continue; /* no interface descriptor */ 27433acd904dSAndrew Thompson 27443acd904dSAndrew Thompson sb = sbuf_new_auto(); 27453acd904dSAndrew Thompson sbuf_printf(sb, 27468bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27478bb77249SHans Petter Selasky "ugen=%s " 274802c40241SHans Petter Selasky "cdev=%s " 27498bb77249SHans Petter Selasky #endif 27503acd904dSAndrew Thompson "vendor=0x%04x " 27513acd904dSAndrew Thompson "product=0x%04x " 27523acd904dSAndrew Thompson "devclass=0x%02x " 27533acd904dSAndrew Thompson "devsubclass=0x%02x " 27543acd904dSAndrew Thompson "sernum=\"%s\" " 27553acd904dSAndrew Thompson "release=0x%04x " 275666390216SAndrew Thompson "mode=%s " 27573acd904dSAndrew Thompson "interface=%d " 27583acd904dSAndrew Thompson "endpoints=%d " 27593acd904dSAndrew Thompson "intclass=0x%02x " 27603acd904dSAndrew Thompson "intsubclass=0x%02x " 27616df3eebbSNick Hibma "intprotocol=0x%02x", 27628bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27633acd904dSAndrew Thompson udev->ugen_name, 276402c40241SHans Petter Selasky udev->ugen_name, 27658bb77249SHans Petter Selasky #endif 27663acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 27673acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 27683acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 27693acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2770ae538d85SAndrew Thompson usb_get_serial(udev), 27713acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 277266390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 27733acd904dSAndrew Thompson iface->idesc->bInterfaceNumber, 27743acd904dSAndrew Thompson iface->idesc->bNumEndpoints, 27753acd904dSAndrew Thompson iface->idesc->bInterfaceClass, 27763acd904dSAndrew Thompson iface->idesc->bInterfaceSubClass, 27773acd904dSAndrew Thompson iface->idesc->bInterfaceProtocol); 27783acd904dSAndrew Thompson sbuf_finish(sb); 27793acd904dSAndrew Thompson devctl_notify("USB", "INTERFACE", type, sbuf_data(sb)); 27803acd904dSAndrew Thompson sbuf_delete(sb); 27813acd904dSAndrew Thompson } 27823acd904dSAndrew Thompson } 27838bb77249SHans Petter Selasky #endif 278402ac6454SAndrew Thompson 27858bb77249SHans Petter Selasky #if USB_HAVE_UGEN 278602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2787a593f6b8SAndrew Thompson * usb_fifo_free_wrap 278802ac6454SAndrew Thompson * 278902ac6454SAndrew Thompson * This function will free the FIFOs. 279002ac6454SAndrew Thompson * 2791bdd41206SAndrew Thompson * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2792bdd41206SAndrew Thompson * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2793bdd41206SAndrew Thompson * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2794bdd41206SAndrew Thompson * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2795bdd41206SAndrew Thompson * control endpoint FIFOs. If "iface_index" is not set to 2796bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" the flag has no effect. 279702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 279802ac6454SAndrew Thompson static void 2799a593f6b8SAndrew Thompson usb_fifo_free_wrap(struct usb_device *udev, 280002ac6454SAndrew Thompson uint8_t iface_index, uint8_t flag) 280102ac6454SAndrew Thompson { 2802760bc48eSAndrew Thompson struct usb_fifo *f; 280302ac6454SAndrew Thompson uint16_t i; 280402ac6454SAndrew Thompson 280502ac6454SAndrew Thompson /* 280602ac6454SAndrew Thompson * Free any USB FIFOs on the given interface: 280702ac6454SAndrew Thompson */ 280802ac6454SAndrew Thompson for (i = 0; i != USB_FIFO_MAX; i++) { 280902ac6454SAndrew Thompson f = udev->fifo[i]; 281002ac6454SAndrew Thompson if (f == NULL) { 281102ac6454SAndrew Thompson continue; 281202ac6454SAndrew Thompson } 281302ac6454SAndrew Thompson /* Check if the interface index matches */ 281402ac6454SAndrew Thompson if (iface_index == f->iface_index) { 2815a593f6b8SAndrew Thompson if (f->methods != &usb_ugen_methods) { 281602ac6454SAndrew Thompson /* 281702ac6454SAndrew Thompson * Don't free any non-generic FIFOs in 281802ac6454SAndrew Thompson * this case. 281902ac6454SAndrew Thompson */ 282002ac6454SAndrew Thompson continue; 282102ac6454SAndrew Thompson } 282202ac6454SAndrew Thompson if ((f->dev_ep_index == 0) && 282302ac6454SAndrew Thompson (f->fs_xfer == NULL)) { 282402ac6454SAndrew Thompson /* no need to free this FIFO */ 282502ac6454SAndrew Thompson continue; 282602ac6454SAndrew Thompson } 282702ac6454SAndrew Thompson } else if (iface_index == USB_IFACE_INDEX_ANY) { 2828a593f6b8SAndrew Thompson if ((f->methods == &usb_ugen_methods) && 2829bdd41206SAndrew Thompson (f->dev_ep_index == 0) && 2830bdd41206SAndrew Thompson (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 283102ac6454SAndrew Thompson (f->fs_xfer == NULL)) { 283202ac6454SAndrew Thompson /* no need to free this FIFO */ 283302ac6454SAndrew Thompson continue; 283402ac6454SAndrew Thompson } 283502ac6454SAndrew Thompson } else { 283602ac6454SAndrew Thompson /* no need to free this FIFO */ 283702ac6454SAndrew Thompson continue; 283802ac6454SAndrew Thompson } 283902ac6454SAndrew Thompson /* free this FIFO */ 2840a593f6b8SAndrew Thompson usb_fifo_free(f); 284102ac6454SAndrew Thompson } 284202ac6454SAndrew Thompson } 28430f6e8f93SAndrew Thompson #endif 284402ac6454SAndrew Thompson 284502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2846a593f6b8SAndrew Thompson * usb_peer_can_wakeup 284702ac6454SAndrew Thompson * 284802ac6454SAndrew Thompson * Return values: 284902ac6454SAndrew Thompson * 0: Peer cannot do resume signalling. 285002ac6454SAndrew Thompson * Else: Peer can do resume signalling. 285102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 285202ac6454SAndrew Thompson uint8_t 2853a593f6b8SAndrew Thompson usb_peer_can_wakeup(struct usb_device *udev) 285402ac6454SAndrew Thompson { 2855760bc48eSAndrew Thompson const struct usb_config_descriptor *cdp; 285602ac6454SAndrew Thompson 285702ac6454SAndrew Thompson cdp = udev->cdesc; 2858f29a0724SAndrew Thompson if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) { 285902ac6454SAndrew Thompson return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 286002ac6454SAndrew Thompson } 286102ac6454SAndrew Thompson return (0); /* not supported */ 286202ac6454SAndrew Thompson } 2863bd216778SAndrew Thompson 2864bd216778SAndrew Thompson void 2865a593f6b8SAndrew Thompson usb_set_device_state(struct usb_device *udev, enum usb_dev_state state) 2866bd216778SAndrew Thompson { 2867bd216778SAndrew Thompson 2868bd216778SAndrew Thompson KASSERT(state < USB_STATE_MAX, ("invalid udev state")); 2869bd216778SAndrew Thompson 2870bd216778SAndrew Thompson DPRINTF("udev %p state %s -> %s\n", udev, 2871a593f6b8SAndrew Thompson usb_statestr(udev->state), usb_statestr(state)); 2872963169b4SHans Petter Selasky 2873f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2874f97da128SHans Petter Selasky mtx_lock(&usb_ref_lock); 2875f97da128SHans Petter Selasky #endif 2876f97da128SHans Petter Selasky udev->state = state; 2877f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2878f97da128SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2879f97da128SHans Petter Selasky #endif 2880963169b4SHans Petter Selasky if (udev->bus->methods->device_state_change != NULL) 2881963169b4SHans Petter Selasky (udev->bus->methods->device_state_change) (udev); 2882963169b4SHans Petter Selasky } 2883963169b4SHans Petter Selasky 2884963169b4SHans Petter Selasky enum usb_dev_state 2885963169b4SHans Petter Selasky usb_get_device_state(struct usb_device *udev) 2886963169b4SHans Petter Selasky { 2887963169b4SHans Petter Selasky if (udev == NULL) 2888963169b4SHans Petter Selasky return (USB_STATE_DETACHED); 2889963169b4SHans Petter Selasky return (udev->state); 2890bd216778SAndrew Thompson } 2891bd216778SAndrew Thompson 2892ec8f3127SAndrew Thompson uint8_t 2893a593f6b8SAndrew Thompson usbd_device_attached(struct usb_device *udev) 2894bd216778SAndrew Thompson { 2895bd216778SAndrew Thompson return (udev->state > USB_STATE_DETACHED); 2896bd216778SAndrew Thompson } 2897cb18f7d1SAlfred Perlstein 28986950c75fSHans Petter Selasky /* 28996950c75fSHans Petter Selasky * The following function locks enumerating the given USB device. If 29006950c75fSHans Petter Selasky * the lock is already grabbed this function returns zero. Else a 290110aab8b6SHans Petter Selasky * a value of one is returned. 29026950c75fSHans Petter Selasky */ 29036950c75fSHans Petter Selasky uint8_t 2904cb18f7d1SAlfred Perlstein usbd_enum_lock(struct usb_device *udev) 2905cb18f7d1SAlfred Perlstein { 29066950c75fSHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 29076950c75fSHans Petter Selasky return (0); 29086950c75fSHans Petter Selasky 290991cd9240SAndrew Thompson sx_xlock(&udev->enum_sx); 29102df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 2911cb18f7d1SAlfred Perlstein /* 2912cb18f7d1SAlfred Perlstein * NEWBUS LOCK NOTE: We should check if any parent SX locks 2913cb18f7d1SAlfred Perlstein * are locked before locking Giant. Else the lock can be 2914cb18f7d1SAlfred Perlstein * locked multiple times. 2915cb18f7d1SAlfred Perlstein */ 2916cb18f7d1SAlfred Perlstein mtx_lock(&Giant); 29176950c75fSHans Petter Selasky return (1); 2918cb18f7d1SAlfred Perlstein } 2919cb18f7d1SAlfred Perlstein 292010aab8b6SHans Petter Selasky #if USB_HAVE_UGEN 292110aab8b6SHans Petter Selasky /* 292210aab8b6SHans Petter Selasky * This function is the same like usbd_enum_lock() except a value of 292310aab8b6SHans Petter Selasky * 255 is returned when a signal is pending: 292410aab8b6SHans Petter Selasky */ 292510aab8b6SHans Petter Selasky uint8_t 292610aab8b6SHans Petter Selasky usbd_enum_lock_sig(struct usb_device *udev) 292710aab8b6SHans Petter Selasky { 292810aab8b6SHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 292910aab8b6SHans Petter Selasky return (0); 293010aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->enum_sx)) 293110aab8b6SHans Petter Selasky return (255); 293210aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->sr_sx)) { 293310aab8b6SHans Petter Selasky sx_xunlock(&udev->enum_sx); 293410aab8b6SHans Petter Selasky return (255); 293510aab8b6SHans Petter Selasky } 293610aab8b6SHans Petter Selasky mtx_lock(&Giant); 293710aab8b6SHans Petter Selasky return (1); 293810aab8b6SHans Petter Selasky } 293910aab8b6SHans Petter Selasky #endif 294010aab8b6SHans Petter Selasky 2941cb18f7d1SAlfred Perlstein /* The following function unlocks enumerating the given USB device. */ 2942cb18f7d1SAlfred Perlstein 2943cb18f7d1SAlfred Perlstein void 2944cb18f7d1SAlfred Perlstein usbd_enum_unlock(struct usb_device *udev) 2945cb18f7d1SAlfred Perlstein { 2946cb18f7d1SAlfred Perlstein mtx_unlock(&Giant); 294791cd9240SAndrew Thompson sx_xunlock(&udev->enum_sx); 29482df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 29492df1e9a6SAndrew Thompson } 29502df1e9a6SAndrew Thompson 29512df1e9a6SAndrew Thompson /* The following function locks suspend and resume. */ 29522df1e9a6SAndrew Thompson 29532df1e9a6SAndrew Thompson void 29542df1e9a6SAndrew Thompson usbd_sr_lock(struct usb_device *udev) 29552df1e9a6SAndrew Thompson { 29562df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 29572df1e9a6SAndrew Thompson /* 29582df1e9a6SAndrew Thompson * NEWBUS LOCK NOTE: We should check if any parent SX locks 29592df1e9a6SAndrew Thompson * are locked before locking Giant. Else the lock can be 29602df1e9a6SAndrew Thompson * locked multiple times. 29612df1e9a6SAndrew Thompson */ 29622df1e9a6SAndrew Thompson mtx_lock(&Giant); 29632df1e9a6SAndrew Thompson } 29642df1e9a6SAndrew Thompson 29652df1e9a6SAndrew Thompson /* The following function unlocks suspend and resume. */ 29662df1e9a6SAndrew Thompson 29672df1e9a6SAndrew Thompson void 29682df1e9a6SAndrew Thompson usbd_sr_unlock(struct usb_device *udev) 29692df1e9a6SAndrew Thompson { 29702df1e9a6SAndrew Thompson mtx_unlock(&Giant); 29712df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 2972cb18f7d1SAlfred Perlstein } 2973cb18f7d1SAlfred Perlstein 2974cb18f7d1SAlfred Perlstein /* 2975cb18f7d1SAlfred Perlstein * The following function checks the enumerating lock for the given 2976cb18f7d1SAlfred Perlstein * USB device. 2977cb18f7d1SAlfred Perlstein */ 2978cb18f7d1SAlfred Perlstein 2979cb18f7d1SAlfred Perlstein uint8_t 2980cb18f7d1SAlfred Perlstein usbd_enum_is_locked(struct usb_device *udev) 2981cb18f7d1SAlfred Perlstein { 298291cd9240SAndrew Thompson return (sx_xlocked(&udev->enum_sx)); 2983cb18f7d1SAlfred Perlstein } 29848427ed84SHans Petter Selasky 29858427ed84SHans Petter Selasky /* 298664cb5e2aSHans Petter Selasky * The following function is used to serialize access to USB control 298764cb5e2aSHans Petter Selasky * transfers and the USB scratch area. If the lock is already grabbed 298864cb5e2aSHans Petter Selasky * this function returns zero. Else a value of one is returned. 298964cb5e2aSHans Petter Selasky */ 299064cb5e2aSHans Petter Selasky uint8_t 299164cb5e2aSHans Petter Selasky usbd_ctrl_lock(struct usb_device *udev) 299264cb5e2aSHans Petter Selasky { 299364cb5e2aSHans Petter Selasky if (sx_xlocked(&udev->ctrl_sx)) 299464cb5e2aSHans Petter Selasky return (0); 299564cb5e2aSHans Petter Selasky sx_xlock(&udev->ctrl_sx); 299664cb5e2aSHans Petter Selasky 299764cb5e2aSHans Petter Selasky /* 299864cb5e2aSHans Petter Selasky * We need to allow suspend and resume at this point, else the 299964cb5e2aSHans Petter Selasky * control transfer will timeout if the device is suspended! 300064cb5e2aSHans Petter Selasky */ 300164cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 300264cb5e2aSHans Petter Selasky usbd_sr_unlock(udev); 300364cb5e2aSHans Petter Selasky return (1); 300464cb5e2aSHans Petter Selasky } 300564cb5e2aSHans Petter Selasky 300664cb5e2aSHans Petter Selasky void 300764cb5e2aSHans Petter Selasky usbd_ctrl_unlock(struct usb_device *udev) 300864cb5e2aSHans Petter Selasky { 300964cb5e2aSHans Petter Selasky sx_xunlock(&udev->ctrl_sx); 301064cb5e2aSHans Petter Selasky 301164cb5e2aSHans Petter Selasky /* 301264cb5e2aSHans Petter Selasky * Restore the suspend and resume lock after we have unlocked 301364cb5e2aSHans Petter Selasky * the USB control transfer lock to avoid LOR: 301464cb5e2aSHans Petter Selasky */ 301564cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 301664cb5e2aSHans Petter Selasky usbd_sr_lock(udev); 301764cb5e2aSHans Petter Selasky } 301864cb5e2aSHans Petter Selasky 301964cb5e2aSHans Petter Selasky /* 30208427ed84SHans Petter Selasky * The following function is used to set the per-interface specific 30218427ed84SHans Petter Selasky * plug and play information. The string referred to by the pnpinfo 30228427ed84SHans Petter Selasky * argument can safely be freed after calling this function. The 30238427ed84SHans Petter Selasky * pnpinfo of an interface will be reset at device detach or when 30248427ed84SHans Petter Selasky * passing a NULL argument to this function. This function 30258427ed84SHans Petter Selasky * returns zero on success, else a USB_ERR_XXX failure code. 30268427ed84SHans Petter Selasky */ 30278427ed84SHans Petter Selasky 30288427ed84SHans Petter Selasky usb_error_t 30298427ed84SHans Petter Selasky usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo) 30308427ed84SHans Petter Selasky { 30318427ed84SHans Petter Selasky struct usb_interface *iface; 30328427ed84SHans Petter Selasky 30338427ed84SHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 30348427ed84SHans Petter Selasky if (iface == NULL) 30358427ed84SHans Petter Selasky return (USB_ERR_INVAL); 30368427ed84SHans Petter Selasky 30378427ed84SHans Petter Selasky if (iface->pnpinfo != NULL) { 30388427ed84SHans Petter Selasky free(iface->pnpinfo, M_USBDEV); 30398427ed84SHans Petter Selasky iface->pnpinfo = NULL; 30408427ed84SHans Petter Selasky } 30418427ed84SHans Petter Selasky 30428427ed84SHans Petter Selasky if (pnpinfo == NULL || pnpinfo[0] == 0) 30438427ed84SHans Petter Selasky return (0); /* success */ 30448427ed84SHans Petter Selasky 30458427ed84SHans Petter Selasky iface->pnpinfo = strdup(pnpinfo, M_USBDEV); 30468427ed84SHans Petter Selasky if (iface->pnpinfo == NULL) 30478427ed84SHans Petter Selasky return (USB_ERR_NOMEM); 30488427ed84SHans Petter Selasky 30498427ed84SHans Petter Selasky return (0); /* success */ 30508427ed84SHans Petter Selasky } 30518427ed84SHans Petter Selasky 3052d46dc4adSHans Petter Selasky usb_error_t 3053d46dc4adSHans Petter Selasky usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk) 3054d46dc4adSHans Petter Selasky { 3055d46dc4adSHans Petter Selasky uint8_t x; 3056d46dc4adSHans Petter Selasky 3057d46dc4adSHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 30583f37fb62SHans Petter Selasky if (udev->autoQuirk[x] == 0 || 30593f37fb62SHans Petter Selasky udev->autoQuirk[x] == quirk) { 3060d46dc4adSHans Petter Selasky udev->autoQuirk[x] = quirk; 3061d46dc4adSHans Petter Selasky return (0); /* success */ 3062d46dc4adSHans Petter Selasky } 3063d46dc4adSHans Petter Selasky } 3064d46dc4adSHans Petter Selasky return (USB_ERR_NOMEM); 3065d46dc4adSHans Petter Selasky } 3066a5cf1aaaSHans Petter Selasky 3067a5cf1aaaSHans Petter Selasky /* 3068a5cf1aaaSHans Petter Selasky * The following function is used to select the endpoint mode. It 3069a5cf1aaaSHans Petter Selasky * should not be called outside enumeration context. 3070a5cf1aaaSHans Petter Selasky */ 3071a5cf1aaaSHans Petter Selasky 3072a5cf1aaaSHans Petter Selasky usb_error_t 3073a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep, 3074a5cf1aaaSHans Petter Selasky uint8_t ep_mode) 3075a5cf1aaaSHans Petter Selasky { 3076a5cf1aaaSHans Petter Selasky usb_error_t error; 3077f61fc4bdSHans Petter Selasky uint8_t do_unlock; 3078a5cf1aaaSHans Petter Selasky 30796950c75fSHans Petter Selasky /* Prevent re-enumeration */ 30806950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 3081a5cf1aaaSHans Petter Selasky 3082a5cf1aaaSHans Petter Selasky if (udev->bus->methods->set_endpoint_mode != NULL) { 3083a5cf1aaaSHans Petter Selasky error = (udev->bus->methods->set_endpoint_mode) ( 3084a5cf1aaaSHans Petter Selasky udev, ep, ep_mode); 3085a5cf1aaaSHans Petter Selasky } else if (ep_mode != USB_EP_MODE_DEFAULT) { 3086a5cf1aaaSHans Petter Selasky error = USB_ERR_INVAL; 3087a5cf1aaaSHans Petter Selasky } else { 3088a5cf1aaaSHans Petter Selasky error = 0; 3089a5cf1aaaSHans Petter Selasky } 3090a5cf1aaaSHans Petter Selasky 3091a5cf1aaaSHans Petter Selasky /* only set new mode regardless of error */ 3092a5cf1aaaSHans Petter Selasky ep->ep_mode = ep_mode; 3093a5cf1aaaSHans Petter Selasky 3094f61fc4bdSHans Petter Selasky if (do_unlock) 3095f61fc4bdSHans Petter Selasky usbd_enum_unlock(udev); 3096a5cf1aaaSHans Petter Selasky return (error); 3097a5cf1aaaSHans Petter Selasky } 3098a5cf1aaaSHans Petter Selasky 3099a5cf1aaaSHans Petter Selasky uint8_t 3100a5cf1aaaSHans Petter Selasky usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep) 3101a5cf1aaaSHans Petter Selasky { 3102a5cf1aaaSHans Petter Selasky return (ep->ep_mode); 3103a5cf1aaaSHans Petter Selasky } 3104