102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 3718cf2ccSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 4718cf2ccSPedro F. Giffuni * 5*9b077d72SHans Petter Selasky * Copyright (c) 2008-2023 Hans Petter Selasky 602ac6454SAndrew Thompson * 702ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 802ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 902ac6454SAndrew Thompson * are met: 1002ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1202ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1302ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1402ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1502ac6454SAndrew Thompson * 1602ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1702ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1802ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1902ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2002ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2102ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2202ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2302ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2402ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2502ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2602ac6454SAndrew Thompson * SUCH DAMAGE. 2702ac6454SAndrew Thompson */ 2802ac6454SAndrew Thompson 29d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 30d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 31d2b99310SHans Petter Selasky #else 32ed6d949aSAndrew Thompson #include <sys/stdint.h> 33ed6d949aSAndrew Thompson #include <sys/stddef.h> 34ed6d949aSAndrew Thompson #include <sys/param.h> 35e2e050c8SConrad Meyer #include <sys/eventhandler.h> 36ed6d949aSAndrew Thompson #include <sys/queue.h> 37ed6d949aSAndrew Thompson #include <sys/systm.h> 38ed6d949aSAndrew Thompson #include <sys/kernel.h> 39ed6d949aSAndrew Thompson #include <sys/bus.h> 40ed6d949aSAndrew Thompson #include <sys/module.h> 41ed6d949aSAndrew Thompson #include <sys/lock.h> 42ed6d949aSAndrew Thompson #include <sys/mutex.h> 43ed6d949aSAndrew Thompson #include <sys/condvar.h> 44ed6d949aSAndrew Thompson #include <sys/sysctl.h> 45ed6d949aSAndrew Thompson #include <sys/sx.h> 46ed6d949aSAndrew Thompson #include <sys/unistd.h> 47ed6d949aSAndrew Thompson #include <sys/callout.h> 48ed6d949aSAndrew Thompson #include <sys/malloc.h> 49ed6d949aSAndrew Thompson #include <sys/priv.h> 50ed6d949aSAndrew Thompson #include <sys/conf.h> 51ed6d949aSAndrew Thompson #include <sys/fcntl.h> 52ed6d949aSAndrew Thompson 5302ac6454SAndrew Thompson #include <dev/usb/usb.h> 54ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 55ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 5602ac6454SAndrew Thompson #include <dev/usb/usb_ioctl.h> 57963169b4SHans Petter Selasky 58963169b4SHans Petter Selasky #if USB_HAVE_UGEN 59963169b4SHans Petter Selasky #include <sys/sbuf.h> 60963169b4SHans Petter Selasky #endif 61963169b4SHans Petter Selasky 6202ac6454SAndrew Thompson #include "usbdevs.h" 6302ac6454SAndrew Thompson 64a593f6b8SAndrew Thompson #define USB_DEBUG_VAR usb_debug 6502ac6454SAndrew Thompson 6602ac6454SAndrew Thompson #include <dev/usb/usb_core.h> 6702ac6454SAndrew Thompson #include <dev/usb/usb_debug.h> 6802ac6454SAndrew Thompson #include <dev/usb/usb_process.h> 6902ac6454SAndrew Thompson #include <dev/usb/usb_device.h> 7002ac6454SAndrew Thompson #include <dev/usb/usb_busdma.h> 7102ac6454SAndrew Thompson #include <dev/usb/usb_transfer.h> 7202ac6454SAndrew Thompson #include <dev/usb/usb_request.h> 7302ac6454SAndrew Thompson #include <dev/usb/usb_dynamic.h> 7402ac6454SAndrew Thompson #include <dev/usb/usb_hub.h> 7502ac6454SAndrew Thompson #include <dev/usb/usb_util.h> 7602ac6454SAndrew Thompson #include <dev/usb/usb_msctest.h> 77bdc081c6SAndrew Thompson #if USB_HAVE_UGEN 78bdc081c6SAndrew Thompson #include <dev/usb/usb_dev.h> 7902ac6454SAndrew Thompson #include <dev/usb/usb_generic.h> 80bdc081c6SAndrew Thompson #endif 8102ac6454SAndrew Thompson 8202ac6454SAndrew Thompson #include <dev/usb/quirk/usb_quirk.h> 8302ac6454SAndrew Thompson 8402ac6454SAndrew Thompson #include <dev/usb/usb_controller.h> 8502ac6454SAndrew Thompson #include <dev/usb/usb_bus.h> 86d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 8702ac6454SAndrew Thompson 8802ac6454SAndrew Thompson /* function prototypes */ 8902ac6454SAndrew Thompson 90f66c3cfcSEdward Tomasz Napierala static int sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS); 91a593f6b8SAndrew Thompson static void usb_init_endpoint(struct usb_device *, uint8_t, 92963169b4SHans Petter Selasky struct usb_endpoint_descriptor *, 93963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *, 94963169b4SHans Petter Selasky struct usb_endpoint *); 95a593f6b8SAndrew Thompson static void usb_unconfigure(struct usb_device *, uint8_t); 96a593f6b8SAndrew Thompson static void usb_detach_device_sub(struct usb_device *, device_t *, 978427ed84SHans Petter Selasky char **, uint8_t); 98a593f6b8SAndrew Thompson static uint8_t usb_probe_and_attach_sub(struct usb_device *, 99760bc48eSAndrew Thompson struct usb_attach_arg *); 100a593f6b8SAndrew Thompson static void usb_init_attach_arg(struct usb_device *, 101760bc48eSAndrew Thompson struct usb_attach_arg *); 102a593f6b8SAndrew Thompson static void usb_suspend_resume_sub(struct usb_device *, device_t, 10302ac6454SAndrew Thompson uint8_t); 104a0d53e0bSHans Petter Selasky static usb_proc_callback_t usbd_clear_stall_proc; 105963169b4SHans Petter Selasky static usb_error_t usb_config_parse(struct usb_device *, uint8_t, uint8_t); 1068bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 107a593f6b8SAndrew Thompson static void usb_notify_addq(const char *type, struct usb_device *); 1088bb77249SHans Petter Selasky #endif 1098bb77249SHans Petter Selasky #if USB_HAVE_UGEN 110a593f6b8SAndrew Thompson static void usb_fifo_free_wrap(struct usb_device *, uint8_t, uint8_t); 111a593f6b8SAndrew Thompson static void usb_cdev_create(struct usb_device *); 112a593f6b8SAndrew Thompson static void usb_cdev_free(struct usb_device *); 1130f6e8f93SAndrew Thompson #endif 11402ac6454SAndrew Thompson 11502ac6454SAndrew Thompson /* This variable is global to allow easy access to it: */ 11602ac6454SAndrew Thompson 117fa49bce5SHans Petter Selasky #ifdef USB_TEMPLATE 118fa49bce5SHans Petter Selasky int usb_template = USB_TEMPLATE; 119fa49bce5SHans Petter Selasky #else 120c140287aSEdward Tomasz Napierala int usb_template = -1; 121fa49bce5SHans Petter Selasky #endif 12202ac6454SAndrew Thompson 123f66c3cfcSEdward Tomasz Napierala SYSCTL_PROC(_hw_usb, OID_AUTO, template, 124f66c3cfcSEdward Tomasz Napierala CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 125f66c3cfcSEdward Tomasz Napierala NULL, 0, sysctl_hw_usb_template, 126f66c3cfcSEdward Tomasz Napierala "I", "Selected USB device side template"); 127f66c3cfcSEdward Tomasz Napierala 128f66c3cfcSEdward Tomasz Napierala /*------------------------------------------------------------------------* 129f66c3cfcSEdward Tomasz Napierala * usb_trigger_reprobe_on_off 130f66c3cfcSEdward Tomasz Napierala * 131f66c3cfcSEdward Tomasz Napierala * This function sets the pull up resistors for all ports currently 132f66c3cfcSEdward Tomasz Napierala * operating in device mode either on (when on_not_off is 1), or off 133f66c3cfcSEdward Tomasz Napierala * (when it's 0). 134f66c3cfcSEdward Tomasz Napierala *------------------------------------------------------------------------*/ 135f66c3cfcSEdward Tomasz Napierala static void 136f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(int on_not_off) 137f66c3cfcSEdward Tomasz Napierala { 138f66c3cfcSEdward Tomasz Napierala struct usb_port_status ps; 139f66c3cfcSEdward Tomasz Napierala struct usb_bus *bus; 140f66c3cfcSEdward Tomasz Napierala struct usb_device *udev; 141f66c3cfcSEdward Tomasz Napierala usb_error_t err; 142f66c3cfcSEdward Tomasz Napierala int do_unlock, max; 143f66c3cfcSEdward Tomasz Napierala 144f66c3cfcSEdward Tomasz Napierala max = devclass_get_maxunit(usb_devclass_ptr); 145f66c3cfcSEdward Tomasz Napierala while (max >= 0) { 146f66c3cfcSEdward Tomasz Napierala mtx_lock(&usb_ref_lock); 147f66c3cfcSEdward Tomasz Napierala bus = devclass_get_softc(usb_devclass_ptr, max); 148f66c3cfcSEdward Tomasz Napierala max--; 149f66c3cfcSEdward Tomasz Napierala 150f66c3cfcSEdward Tomasz Napierala if (bus == NULL || bus->devices == NULL || 151f66c3cfcSEdward Tomasz Napierala bus->devices[USB_ROOT_HUB_ADDR] == NULL) { 152f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 153f66c3cfcSEdward Tomasz Napierala continue; 154f66c3cfcSEdward Tomasz Napierala } 155f66c3cfcSEdward Tomasz Napierala 156f66c3cfcSEdward Tomasz Napierala udev = bus->devices[USB_ROOT_HUB_ADDR]; 157f66c3cfcSEdward Tomasz Napierala 158f66c3cfcSEdward Tomasz Napierala if (udev->refcount == USB_DEV_REF_MAX) { 159f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 160f66c3cfcSEdward Tomasz Napierala continue; 161f66c3cfcSEdward Tomasz Napierala } 162f66c3cfcSEdward Tomasz Napierala 163f66c3cfcSEdward Tomasz Napierala udev->refcount++; 164f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 165f66c3cfcSEdward Tomasz Napierala 166f66c3cfcSEdward Tomasz Napierala do_unlock = usbd_enum_lock(udev); 167f66c3cfcSEdward Tomasz Napierala if (do_unlock > 1) { 168f66c3cfcSEdward Tomasz Napierala do_unlock = 0; 169f66c3cfcSEdward Tomasz Napierala goto next; 170f66c3cfcSEdward Tomasz Napierala } 171f66c3cfcSEdward Tomasz Napierala 172f66c3cfcSEdward Tomasz Napierala err = usbd_req_get_port_status(udev, NULL, &ps, 1); 173f66c3cfcSEdward Tomasz Napierala if (err != 0) { 174f66c3cfcSEdward Tomasz Napierala DPRINTF("usbd_req_get_port_status() " 175f66c3cfcSEdward Tomasz Napierala "failed: %s\n", usbd_errstr(err)); 176f66c3cfcSEdward Tomasz Napierala goto next; 177f66c3cfcSEdward Tomasz Napierala } 178f66c3cfcSEdward Tomasz Napierala 179f66c3cfcSEdward Tomasz Napierala if ((UGETW(ps.wPortStatus) & UPS_PORT_MODE_DEVICE) == 0) 180f66c3cfcSEdward Tomasz Napierala goto next; 181f66c3cfcSEdward Tomasz Napierala 182f66c3cfcSEdward Tomasz Napierala if (on_not_off) { 183f66c3cfcSEdward Tomasz Napierala err = usbd_req_set_port_feature(udev, NULL, 1, 184f66c3cfcSEdward Tomasz Napierala UHF_PORT_POWER); 185f66c3cfcSEdward Tomasz Napierala if (err != 0) { 186f66c3cfcSEdward Tomasz Napierala DPRINTF("usbd_req_set_port_feature() " 187f66c3cfcSEdward Tomasz Napierala "failed: %s\n", usbd_errstr(err)); 188f66c3cfcSEdward Tomasz Napierala } 189f66c3cfcSEdward Tomasz Napierala } else { 190f66c3cfcSEdward Tomasz Napierala err = usbd_req_clear_port_feature(udev, NULL, 1, 191f66c3cfcSEdward Tomasz Napierala UHF_PORT_POWER); 192f66c3cfcSEdward Tomasz Napierala if (err != 0) { 193f66c3cfcSEdward Tomasz Napierala DPRINTF("usbd_req_clear_port_feature() " 194f66c3cfcSEdward Tomasz Napierala "failed: %s\n", usbd_errstr(err)); 195f66c3cfcSEdward Tomasz Napierala } 196f66c3cfcSEdward Tomasz Napierala } 197f66c3cfcSEdward Tomasz Napierala 198f66c3cfcSEdward Tomasz Napierala next: 199f66c3cfcSEdward Tomasz Napierala mtx_lock(&usb_ref_lock); 200f66c3cfcSEdward Tomasz Napierala if (do_unlock) 201f66c3cfcSEdward Tomasz Napierala usbd_enum_unlock(udev); 202f66c3cfcSEdward Tomasz Napierala if (--(udev->refcount) == 0) 203f66c3cfcSEdward Tomasz Napierala cv_broadcast(&udev->ref_cv); 204f66c3cfcSEdward Tomasz Napierala mtx_unlock(&usb_ref_lock); 205f66c3cfcSEdward Tomasz Napierala } 206f66c3cfcSEdward Tomasz Napierala } 207f66c3cfcSEdward Tomasz Napierala 208f66c3cfcSEdward Tomasz Napierala /*------------------------------------------------------------------------* 209f66c3cfcSEdward Tomasz Napierala * usb_trigger_reprobe_all 210f66c3cfcSEdward Tomasz Napierala * 211f66c3cfcSEdward Tomasz Napierala * This function toggles the pull up resistors for all ports currently 212f66c3cfcSEdward Tomasz Napierala * operating in device mode, causing the host machine to reenumerate them. 213f66c3cfcSEdward Tomasz Napierala *------------------------------------------------------------------------*/ 214f66c3cfcSEdward Tomasz Napierala static void 215f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_all(void) 216f66c3cfcSEdward Tomasz Napierala { 217f66c3cfcSEdward Tomasz Napierala 218f66c3cfcSEdward Tomasz Napierala /* 219f66c3cfcSEdward Tomasz Napierala * Set the pull up resistors off for all ports in device mode. 220f66c3cfcSEdward Tomasz Napierala */ 221f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(0); 222f66c3cfcSEdward Tomasz Napierala 223f66c3cfcSEdward Tomasz Napierala /* 224f66c3cfcSEdward Tomasz Napierala * According to the DWC OTG spec this must be at least 3ms. 225f66c3cfcSEdward Tomasz Napierala */ 226f66c3cfcSEdward Tomasz Napierala usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME)); 227f66c3cfcSEdward Tomasz Napierala 228f66c3cfcSEdward Tomasz Napierala /* 229f66c3cfcSEdward Tomasz Napierala * Set the pull up resistors back on. 230f66c3cfcSEdward Tomasz Napierala */ 231f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(1); 232f66c3cfcSEdward Tomasz Napierala } 233f66c3cfcSEdward Tomasz Napierala 234f66c3cfcSEdward Tomasz Napierala static int 235f66c3cfcSEdward Tomasz Napierala sysctl_hw_usb_template(SYSCTL_HANDLER_ARGS) 236f66c3cfcSEdward Tomasz Napierala { 237f66c3cfcSEdward Tomasz Napierala int error, val; 238f66c3cfcSEdward Tomasz Napierala 239f66c3cfcSEdward Tomasz Napierala val = usb_template; 240f66c3cfcSEdward Tomasz Napierala error = sysctl_handle_int(oidp, &val, 0, req); 241f66c3cfcSEdward Tomasz Napierala if (error != 0 || req->newptr == NULL || usb_template == val) 242f66c3cfcSEdward Tomasz Napierala return (error); 243f66c3cfcSEdward Tomasz Napierala 244f66c3cfcSEdward Tomasz Napierala usb_template = val; 245f66c3cfcSEdward Tomasz Napierala 246f66c3cfcSEdward Tomasz Napierala if (usb_template < 0) { 247f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_on_off(0); 248f66c3cfcSEdward Tomasz Napierala } else { 249f66c3cfcSEdward Tomasz Napierala usb_trigger_reprobe_all(); 250f66c3cfcSEdward Tomasz Napierala } 251f66c3cfcSEdward Tomasz Napierala 252f66c3cfcSEdward Tomasz Napierala return (0); 253f66c3cfcSEdward Tomasz Napierala } 25402ac6454SAndrew Thompson 2557a918edfSAndrew Thompson /* English is default language */ 2567a918edfSAndrew Thompson 2577a918edfSAndrew Thompson static int usb_lang_id = 0x0009; 2587a918edfSAndrew Thompson static int usb_lang_mask = 0x00FF; 2597a918edfSAndrew Thompson 260af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_id, CTLFLAG_RWTUN, 2617a918edfSAndrew Thompson &usb_lang_id, 0, "Preferred USB language ID"); 2627a918edfSAndrew Thompson 263af3b2549SHans Petter Selasky SYSCTL_INT(_hw_usb, OID_AUTO, usb_lang_mask, CTLFLAG_RWTUN, 2647a918edfSAndrew Thompson &usb_lang_mask, 0, "Preferred USB language mask"); 2657a918edfSAndrew Thompson 2664d4fa6edSAndrew Thompson static const char* statestr[USB_STATE_MAX] = { 2674d4fa6edSAndrew Thompson [USB_STATE_DETACHED] = "DETACHED", 2684d4fa6edSAndrew Thompson [USB_STATE_ATTACHED] = "ATTACHED", 2694d4fa6edSAndrew Thompson [USB_STATE_POWERED] = "POWERED", 2704d4fa6edSAndrew Thompson [USB_STATE_ADDRESSED] = "ADDRESSED", 2714d4fa6edSAndrew Thompson [USB_STATE_CONFIGURED] = "CONFIGURED", 2724d4fa6edSAndrew Thompson }; 2734d4fa6edSAndrew Thompson 2744d4fa6edSAndrew Thompson const char * 275a593f6b8SAndrew Thompson usb_statestr(enum usb_dev_state state) 2764d4fa6edSAndrew Thompson { 277ec8f3127SAndrew Thompson return ((state < USB_STATE_MAX) ? statestr[state] : "UNKNOWN"); 2784d4fa6edSAndrew Thompson } 27902ac6454SAndrew Thompson 280ae538d85SAndrew Thompson const char * 281ae538d85SAndrew Thompson usb_get_manufacturer(struct usb_device *udev) 282ae538d85SAndrew Thompson { 283ae538d85SAndrew Thompson return (udev->manufacturer ? udev->manufacturer : "Unknown"); 284ae538d85SAndrew Thompson } 285ae538d85SAndrew Thompson 286ae538d85SAndrew Thompson const char * 287ae538d85SAndrew Thompson usb_get_product(struct usb_device *udev) 288ae538d85SAndrew Thompson { 289ae538d85SAndrew Thompson return (udev->product ? udev->product : ""); 290ae538d85SAndrew Thompson } 291ae538d85SAndrew Thompson 292ae538d85SAndrew Thompson const char * 293ae538d85SAndrew Thompson usb_get_serial(struct usb_device *udev) 294ae538d85SAndrew Thompson { 295ae538d85SAndrew Thompson return (udev->serial ? udev->serial : ""); 296ae538d85SAndrew Thompson } 297ae538d85SAndrew Thompson 29802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 299a593f6b8SAndrew Thompson * usbd_get_ep_by_addr 30002ac6454SAndrew Thompson * 301ae60fdfbSAndrew Thompson * This function searches for an USB ep by endpoint address and 30202ac6454SAndrew Thompson * direction. 30302ac6454SAndrew Thompson * 30402ac6454SAndrew Thompson * Returns: 30502ac6454SAndrew Thompson * NULL: Failure 30602ac6454SAndrew Thompson * Else: Success 30702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 308ae60fdfbSAndrew Thompson struct usb_endpoint * 309a593f6b8SAndrew Thompson usbd_get_ep_by_addr(struct usb_device *udev, uint8_t ea_val) 31002ac6454SAndrew Thompson { 311ae60fdfbSAndrew Thompson struct usb_endpoint *ep = udev->endpoints; 312ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 31302ac6454SAndrew Thompson enum { 31402ac6454SAndrew Thompson EA_MASK = (UE_DIR_IN | UE_DIR_OUT | UE_ADDR), 31502ac6454SAndrew Thompson }; 31602ac6454SAndrew Thompson 31702ac6454SAndrew Thompson /* 31802ac6454SAndrew Thompson * According to the USB specification not all bits are used 31902ac6454SAndrew Thompson * for the endpoint address. Keep defined bits only: 32002ac6454SAndrew Thompson */ 32102ac6454SAndrew Thompson ea_val &= EA_MASK; 32202ac6454SAndrew Thompson 32302ac6454SAndrew Thompson /* 32420733245SPedro F. Giffuni * Iterate across all the USB endpoints searching for a match 32502ac6454SAndrew Thompson * based on the endpoint address: 32602ac6454SAndrew Thompson */ 327ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 328ae60fdfbSAndrew Thompson if (ep->edesc == NULL) { 32902ac6454SAndrew Thompson continue; 33002ac6454SAndrew Thompson } 33102ac6454SAndrew Thompson /* do the mask and check the value */ 332ae60fdfbSAndrew Thompson if ((ep->edesc->bEndpointAddress & EA_MASK) == ea_val) { 33302ac6454SAndrew Thompson goto found; 33402ac6454SAndrew Thompson } 33502ac6454SAndrew Thompson } 33602ac6454SAndrew Thompson 33702ac6454SAndrew Thompson /* 338ae60fdfbSAndrew Thompson * The default endpoint is always present and is checked separately: 33902ac6454SAndrew Thompson */ 34022039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 3415b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & EA_MASK) == ea_val)) { 3425b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 34302ac6454SAndrew Thompson goto found; 34402ac6454SAndrew Thompson } 34502ac6454SAndrew Thompson return (NULL); 34602ac6454SAndrew Thompson 34702ac6454SAndrew Thompson found: 348ae60fdfbSAndrew Thompson return (ep); 34902ac6454SAndrew Thompson } 35002ac6454SAndrew Thompson 35102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 352a593f6b8SAndrew Thompson * usbd_get_endpoint 35302ac6454SAndrew Thompson * 354ae60fdfbSAndrew Thompson * This function searches for an USB endpoint based on the information 355760bc48eSAndrew Thompson * given by the passed "struct usb_config" pointer. 35602ac6454SAndrew Thompson * 35702ac6454SAndrew Thompson * Return values: 35802ac6454SAndrew Thompson * NULL: No match. 359ae60fdfbSAndrew Thompson * Else: Pointer to "struct usb_endpoint". 36002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 361ae60fdfbSAndrew Thompson struct usb_endpoint * 362a593f6b8SAndrew Thompson usbd_get_endpoint(struct usb_device *udev, uint8_t iface_index, 363760bc48eSAndrew Thompson const struct usb_config *setup) 36402ac6454SAndrew Thompson { 365ae60fdfbSAndrew Thompson struct usb_endpoint *ep = udev->endpoints; 366ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end = udev->endpoints + udev->endpoints_max; 36702ac6454SAndrew Thompson uint8_t index = setup->ep_index; 36802ac6454SAndrew Thompson uint8_t ea_mask; 36902ac6454SAndrew Thompson uint8_t ea_val; 37002ac6454SAndrew Thompson uint8_t type_mask; 37102ac6454SAndrew Thompson uint8_t type_val; 37202ac6454SAndrew Thompson 37302ac6454SAndrew Thompson DPRINTFN(10, "udev=%p iface_index=%d address=0x%x " 37402ac6454SAndrew Thompson "type=0x%x dir=0x%x index=%d\n", 37502ac6454SAndrew Thompson udev, iface_index, setup->endpoint, 37602ac6454SAndrew Thompson setup->type, setup->direction, setup->ep_index); 37702ac6454SAndrew Thompson 3784eae601eSAndrew Thompson /* check USB mode */ 3794eae601eSAndrew Thompson 380f29a0724SAndrew Thompson if (setup->usb_mode != USB_MODE_DUAL && 381f29a0724SAndrew Thompson udev->flags.usb_mode != setup->usb_mode) { 382ae60fdfbSAndrew Thompson /* wrong mode - no endpoint */ 3834eae601eSAndrew Thompson return (NULL); 3844eae601eSAndrew Thompson } 3854eae601eSAndrew Thompson 38602ac6454SAndrew Thompson /* setup expected endpoint direction mask and value */ 38702ac6454SAndrew Thompson 38851ec1603SAndrew Thompson if (setup->direction == UE_DIR_RX) { 38951ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 390f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 39151ec1603SAndrew Thompson UE_DIR_OUT : UE_DIR_IN; 39251ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_TX) { 39351ec1603SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 394f29a0724SAndrew Thompson ea_val = (udev->flags.usb_mode == USB_MODE_DEVICE) ? 39551ec1603SAndrew Thompson UE_DIR_IN : UE_DIR_OUT; 39651ec1603SAndrew Thompson } else if (setup->direction == UE_DIR_ANY) { 39702ac6454SAndrew Thompson /* match any endpoint direction */ 39802ac6454SAndrew Thompson ea_mask = 0; 39902ac6454SAndrew Thompson ea_val = 0; 40002ac6454SAndrew Thompson } else { 40102ac6454SAndrew Thompson /* match the given endpoint direction */ 40202ac6454SAndrew Thompson ea_mask = (UE_DIR_IN | UE_DIR_OUT); 40302ac6454SAndrew Thompson ea_val = (setup->direction & (UE_DIR_IN | UE_DIR_OUT)); 40402ac6454SAndrew Thompson } 40502ac6454SAndrew Thompson 40602ac6454SAndrew Thompson /* setup expected endpoint address */ 40702ac6454SAndrew Thompson 40802ac6454SAndrew Thompson if (setup->endpoint == UE_ADDR_ANY) { 40902ac6454SAndrew Thompson /* match any endpoint address */ 41002ac6454SAndrew Thompson } else { 41102ac6454SAndrew Thompson /* match the given endpoint address */ 41202ac6454SAndrew Thompson ea_mask |= UE_ADDR; 41302ac6454SAndrew Thompson ea_val |= (setup->endpoint & UE_ADDR); 41402ac6454SAndrew Thompson } 41502ac6454SAndrew Thompson 41602ac6454SAndrew Thompson /* setup expected endpoint type */ 41702ac6454SAndrew Thompson 41802ac6454SAndrew Thompson if (setup->type == UE_BULK_INTR) { 41902ac6454SAndrew Thompson /* this will match BULK and INTERRUPT endpoints */ 42002ac6454SAndrew Thompson type_mask = 2; 42102ac6454SAndrew Thompson type_val = 2; 42202ac6454SAndrew Thompson } else if (setup->type == UE_TYPE_ANY) { 42302ac6454SAndrew Thompson /* match any endpoint type */ 42402ac6454SAndrew Thompson type_mask = 0; 42502ac6454SAndrew Thompson type_val = 0; 42602ac6454SAndrew Thompson } else { 42702ac6454SAndrew Thompson /* match the given endpoint type */ 42802ac6454SAndrew Thompson type_mask = UE_XFERTYPE; 42902ac6454SAndrew Thompson type_val = (setup->type & UE_XFERTYPE); 43002ac6454SAndrew Thompson } 43102ac6454SAndrew Thompson 43202ac6454SAndrew Thompson /* 43320733245SPedro F. Giffuni * Iterate across all the USB endpoints searching for a match 43402ac6454SAndrew Thompson * based on the endpoint address. Note that we are searching 435ae60fdfbSAndrew Thompson * the endpoints from the beginning of the "udev->endpoints" array. 43602ac6454SAndrew Thompson */ 437ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 438ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 439ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 44002ac6454SAndrew Thompson continue; 44102ac6454SAndrew Thompson } 44202ac6454SAndrew Thompson /* do the masks and check the values */ 44302ac6454SAndrew Thompson 444ae60fdfbSAndrew Thompson if (((ep->edesc->bEndpointAddress & ea_mask) == ea_val) && 445ae60fdfbSAndrew Thompson ((ep->edesc->bmAttributes & type_mask) == type_val)) { 44602ac6454SAndrew Thompson if (!index--) { 44702ac6454SAndrew Thompson goto found; 44802ac6454SAndrew Thompson } 44902ac6454SAndrew Thompson } 45002ac6454SAndrew Thompson } 45102ac6454SAndrew Thompson 45202ac6454SAndrew Thompson /* 453ae60fdfbSAndrew Thompson * Match against default endpoint last, so that "any endpoint", "any 454ae60fdfbSAndrew Thompson * address" and "any direction" returns the first endpoint of the 45502ac6454SAndrew Thompson * interface. "iface_index" and "direction" is ignored: 45602ac6454SAndrew Thompson */ 45722039494SHans Petter Selasky if ((udev->ctrl_ep.edesc != NULL) && 4585b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bEndpointAddress & ea_mask) == ea_val) && 4595b3bb704SAndrew Thompson ((udev->ctrl_ep.edesc->bmAttributes & type_mask) == type_val) && 46002ac6454SAndrew Thompson (!index)) { 4615b3bb704SAndrew Thompson ep = &udev->ctrl_ep; 46202ac6454SAndrew Thompson goto found; 46302ac6454SAndrew Thompson } 46402ac6454SAndrew Thompson return (NULL); 46502ac6454SAndrew Thompson 46602ac6454SAndrew Thompson found: 467ae60fdfbSAndrew Thompson return (ep); 46802ac6454SAndrew Thompson } 46902ac6454SAndrew Thompson 47002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 471ed6d949aSAndrew Thompson * usbd_interface_count 47202ac6454SAndrew Thompson * 47302ac6454SAndrew Thompson * This function stores the number of USB interfaces excluding 47402ac6454SAndrew Thompson * alternate settings, which the USB config descriptor reports into 47502ac6454SAndrew Thompson * the unsigned 8-bit integer pointed to by "count". 47602ac6454SAndrew Thompson * 47702ac6454SAndrew Thompson * Returns: 47802ac6454SAndrew Thompson * 0: Success 47902ac6454SAndrew Thompson * Else: Failure 48002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 481e0a69b51SAndrew Thompson usb_error_t 482ed6d949aSAndrew Thompson usbd_interface_count(struct usb_device *udev, uint8_t *count) 48302ac6454SAndrew Thompson { 48402ac6454SAndrew Thompson if (udev->cdesc == NULL) { 48502ac6454SAndrew Thompson *count = 0; 48602ac6454SAndrew Thompson return (USB_ERR_NOT_CONFIGURED); 48702ac6454SAndrew Thompson } 488bdd41206SAndrew Thompson *count = udev->ifaces_max; 48902ac6454SAndrew Thompson return (USB_ERR_NORMAL_COMPLETION); 49002ac6454SAndrew Thompson } 49102ac6454SAndrew Thompson 49202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 493a593f6b8SAndrew Thompson * usb_init_endpoint 49402ac6454SAndrew Thompson * 495ae60fdfbSAndrew Thompson * This function will initialise the USB endpoint structure pointed to by 496ae60fdfbSAndrew Thompson * the "endpoint" argument. The structure pointed to by "endpoint" must be 497bdd41206SAndrew Thompson * zeroed before calling this function. 49802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 49902ac6454SAndrew Thompson static void 500a593f6b8SAndrew Thompson usb_init_endpoint(struct usb_device *udev, uint8_t iface_index, 501963169b4SHans Petter Selasky struct usb_endpoint_descriptor *edesc, 502963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *ecomp, 503963169b4SHans Petter Selasky struct usb_endpoint *ep) 50402ac6454SAndrew Thompson { 505e892b3feSHans Petter Selasky const struct usb_bus_methods *methods; 506a5cf1aaaSHans Petter Selasky usb_stream_t x; 507ee3e3ff5SAndrew Thompson 508bdd41206SAndrew Thompson methods = udev->bus->methods; 50902ac6454SAndrew Thompson 510ae60fdfbSAndrew Thompson (methods->endpoint_init) (udev, edesc, ep); 51102ac6454SAndrew Thompson 512ae60fdfbSAndrew Thompson /* initialise USB endpoint structure */ 513ae60fdfbSAndrew Thompson ep->edesc = edesc; 514963169b4SHans Petter Selasky ep->ecomp = ecomp; 515ae60fdfbSAndrew Thompson ep->iface_index = iface_index; 516a5cf1aaaSHans Petter Selasky 517a5cf1aaaSHans Petter Selasky /* setup USB stream queues */ 518a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 519a5cf1aaaSHans Petter Selasky TAILQ_INIT(&ep->endpoint_q[x].head); 520a5cf1aaaSHans Petter Selasky ep->endpoint_q[x].command = &usbd_pipe_start; 521a5cf1aaaSHans Petter Selasky } 52202ac6454SAndrew Thompson 52339307315SAndrew Thompson /* the pipe is not supported by the hardware */ 524ae60fdfbSAndrew Thompson if (ep->methods == NULL) 52539307315SAndrew Thompson return; 52639307315SAndrew Thompson 527a5cf1aaaSHans Petter Selasky /* check for SUPER-speed streams mode endpoint */ 528a5cf1aaaSHans Petter Selasky if (udev->speed == USB_SPEED_SUPER && ecomp != NULL && 529a5cf1aaaSHans Petter Selasky (edesc->bmAttributes & UE_XFERTYPE) == UE_BULK && 530a5cf1aaaSHans Petter Selasky (UE_GET_BULK_STREAMS(ecomp->bmAttributes) != 0)) { 531a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_STREAMS); 532a5cf1aaaSHans Petter Selasky } else { 533a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(udev, ep, USB_EP_MODE_DEFAULT); 534a5cf1aaaSHans Petter Selasky } 535a5cf1aaaSHans Petter Selasky 53602ac6454SAndrew Thompson /* clear stall, if any */ 537bdd41206SAndrew Thompson if (methods->clear_stall != NULL) { 53802ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 539ae60fdfbSAndrew Thompson (methods->clear_stall) (udev, ep); 54002ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 54102ac6454SAndrew Thompson } 54202ac6454SAndrew Thompson } 54302ac6454SAndrew Thompson 544bdd41206SAndrew Thompson /*-----------------------------------------------------------------------* 545a593f6b8SAndrew Thompson * usb_endpoint_foreach 54616589beaSAndrew Thompson * 54716589beaSAndrew Thompson * This function will iterate all the USB endpoints except the control 54816589beaSAndrew Thompson * endpoint. This function is NULL safe. 54916589beaSAndrew Thompson * 55016589beaSAndrew Thompson * Return values: 551ae60fdfbSAndrew Thompson * NULL: End of USB endpoints 552ae60fdfbSAndrew Thompson * Else: Pointer to next USB endpoint 55316589beaSAndrew Thompson *------------------------------------------------------------------------*/ 554ae60fdfbSAndrew Thompson struct usb_endpoint * 555a593f6b8SAndrew Thompson usb_endpoint_foreach(struct usb_device *udev, struct usb_endpoint *ep) 55616589beaSAndrew Thompson { 557a9e26757SAndrew Thompson struct usb_endpoint *ep_end; 55816589beaSAndrew Thompson 55916589beaSAndrew Thompson /* be NULL safe */ 56016589beaSAndrew Thompson if (udev == NULL) 56116589beaSAndrew Thompson return (NULL); 56216589beaSAndrew Thompson 563a9e26757SAndrew Thompson ep_end = udev->endpoints + udev->endpoints_max; 564a9e26757SAndrew Thompson 565ae60fdfbSAndrew Thompson /* get next endpoint */ 566ae60fdfbSAndrew Thompson if (ep == NULL) 567ae60fdfbSAndrew Thompson ep = udev->endpoints; 56816589beaSAndrew Thompson else 569ae60fdfbSAndrew Thompson ep++; 57016589beaSAndrew Thompson 571ae60fdfbSAndrew Thompson /* find next allocated ep */ 572ae60fdfbSAndrew Thompson while (ep != ep_end) { 573ae60fdfbSAndrew Thompson if (ep->edesc != NULL) 574ae60fdfbSAndrew Thompson return (ep); 575ae60fdfbSAndrew Thompson ep++; 57616589beaSAndrew Thompson } 57716589beaSAndrew Thompson return (NULL); 57816589beaSAndrew Thompson } 57916589beaSAndrew Thompson 58016589beaSAndrew Thompson /*------------------------------------------------------------------------* 581b78e84d1SHans Petter Selasky * usb_wait_pending_refs 582d008478eSHans Petter Selasky * 583d008478eSHans Petter Selasky * This function will wait for any USB references to go away before 584b78e84d1SHans Petter Selasky * returning. This function is used before freeing a USB device. 585d008478eSHans Petter Selasky *------------------------------------------------------------------------*/ 586d008478eSHans Petter Selasky static void 587b78e84d1SHans Petter Selasky usb_wait_pending_refs(struct usb_device *udev) 588d008478eSHans Petter Selasky { 589d008478eSHans Petter Selasky #if USB_HAVE_UGEN 590b78e84d1SHans Petter Selasky DPRINTF("Refcount = %d\n", (int)udev->refcount); 591d008478eSHans Petter Selasky 592b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 593b78e84d1SHans Petter Selasky udev->refcount--; 594d008478eSHans Petter Selasky while (1) { 595d008478eSHans Petter Selasky /* wait for any pending references to go away */ 596b78e84d1SHans Petter Selasky if (udev->refcount == 0) { 597b78e84d1SHans Petter Selasky /* prevent further refs being taken, if any */ 598d008478eSHans Petter Selasky udev->refcount = USB_DEV_REF_MAX; 599d008478eSHans Petter Selasky break; 600d008478eSHans Petter Selasky } 601d008478eSHans Petter Selasky cv_wait(&udev->ref_cv, &usb_ref_lock); 602d008478eSHans Petter Selasky } 603d008478eSHans Petter Selasky mtx_unlock(&usb_ref_lock); 604d008478eSHans Petter Selasky #endif 605d008478eSHans Petter Selasky } 606d008478eSHans Petter Selasky 607d008478eSHans Petter Selasky /*------------------------------------------------------------------------* 608a593f6b8SAndrew Thompson * usb_unconfigure 60902ac6454SAndrew Thompson * 610ae60fdfbSAndrew Thompson * This function will free all USB interfaces and USB endpoints belonging 61102ac6454SAndrew Thompson * to an USB device. 612bdd41206SAndrew Thompson * 613bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 61402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 61502ac6454SAndrew Thompson static void 616a593f6b8SAndrew Thompson usb_unconfigure(struct usb_device *udev, uint8_t flag) 61702ac6454SAndrew Thompson { 618bdd41206SAndrew Thompson uint8_t do_unlock; 61902ac6454SAndrew Thompson 6206950c75fSHans Petter Selasky /* Prevent re-enumeration */ 6216950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 622bdd41206SAndrew Thompson 623bdd41206SAndrew Thompson /* detach all interface drivers */ 624a593f6b8SAndrew Thompson usb_detach_device(udev, USB_IFACE_INDEX_ANY, flag); 625bdd41206SAndrew Thompson 626bdd41206SAndrew Thompson #if USB_HAVE_UGEN 627bdd41206SAndrew Thompson /* free all FIFOs except control endpoint FIFOs */ 628a593f6b8SAndrew Thompson usb_fifo_free_wrap(udev, USB_IFACE_INDEX_ANY, flag); 629bdd41206SAndrew Thompson 630bdd41206SAndrew Thompson /* 631bdd41206SAndrew Thompson * Free all cdev's, if any. 632bdd41206SAndrew Thompson */ 633a593f6b8SAndrew Thompson usb_cdev_free(udev); 634bdd41206SAndrew Thompson #endif 63502ac6454SAndrew Thompson 636bdc081c6SAndrew Thompson #if USB_HAVE_COMPAT_LINUX 63702ac6454SAndrew Thompson /* free Linux compat device, if any */ 6388d59ecb2SHans Petter Selasky if (udev->linux_endpoint_start != NULL) { 6398d59ecb2SHans Petter Selasky usb_linux_free_device_p(udev); 640760bc48eSAndrew Thompson udev->linux_endpoint_start = NULL; 64102ac6454SAndrew Thompson } 642bdc081c6SAndrew Thompson #endif 64302ac6454SAndrew Thompson 644a593f6b8SAndrew Thompson usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_FREE); 64502ac6454SAndrew Thompson 646ae60fdfbSAndrew Thompson /* free "cdesc" after "ifaces" and "endpoints", if any */ 6477efaaa9aSAndrew Thompson if (udev->cdesc != NULL) { 648f29a0724SAndrew Thompson if (udev->flags.usb_mode != USB_MODE_DEVICE) 6492c79a775SHans Petter Selasky usbd_free_config_desc(udev, udev->cdesc); 65002ac6454SAndrew Thompson udev->cdesc = NULL; 65102ac6454SAndrew Thompson } 65202ac6454SAndrew Thompson /* set unconfigured state */ 65302ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 65402ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 655bdd41206SAndrew Thompson 656cb18f7d1SAlfred Perlstein if (do_unlock) 657cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 65802ac6454SAndrew Thompson } 65902ac6454SAndrew Thompson 66002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 661a593f6b8SAndrew Thompson * usbd_set_config_index 66202ac6454SAndrew Thompson * 66302ac6454SAndrew Thompson * This function selects configuration by index, independent of the 66402ac6454SAndrew Thompson * actual configuration number. This function should not be used by 66502ac6454SAndrew Thompson * USB drivers. 66602ac6454SAndrew Thompson * 66702ac6454SAndrew Thompson * Returns: 66802ac6454SAndrew Thompson * 0: Success 66902ac6454SAndrew Thompson * Else: Failure 67002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 671e0a69b51SAndrew Thompson usb_error_t 672a593f6b8SAndrew Thompson usbd_set_config_index(struct usb_device *udev, uint8_t index) 67302ac6454SAndrew Thompson { 674760bc48eSAndrew Thompson struct usb_status ds; 675760bc48eSAndrew Thompson struct usb_config_descriptor *cdp; 67602ac6454SAndrew Thompson uint16_t power; 67702ac6454SAndrew Thompson uint16_t max_power; 67802ac6454SAndrew Thompson uint8_t selfpowered; 67902ac6454SAndrew Thompson uint8_t do_unlock; 680e0a69b51SAndrew Thompson usb_error_t err; 68102ac6454SAndrew Thompson 68202ac6454SAndrew Thompson DPRINTFN(6, "udev=%p index=%d\n", udev, index); 68302ac6454SAndrew Thompson 6846950c75fSHans Petter Selasky /* Prevent re-enumeration */ 6856950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 68602ac6454SAndrew Thompson 687d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 68802ac6454SAndrew Thompson 68902ac6454SAndrew Thompson if (index == USB_UNCONFIG_INDEX) { 69002ac6454SAndrew Thompson /* 69102ac6454SAndrew Thompson * Leave unallocated when unconfiguring the 692a593f6b8SAndrew Thompson * device. "usb_unconfigure()" will also reset 69302ac6454SAndrew Thompson * the current config number and index. 69402ac6454SAndrew Thompson */ 695a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, USB_UNCONFIG_NO); 696bd216778SAndrew Thompson if (udev->state == USB_STATE_CONFIGURED) 697a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 69802ac6454SAndrew Thompson goto done; 69902ac6454SAndrew Thompson } 70002ac6454SAndrew Thompson /* get the full config descriptor */ 701f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 7027efaaa9aSAndrew Thompson /* save some memory */ 703a593f6b8SAndrew Thompson err = usbd_req_get_descriptor_ptr(udev, &cdp, 704459d369eSAndrew Thompson (UDESC_CONFIG << 8) | index); 7057efaaa9aSAndrew Thompson } else { 7067efaaa9aSAndrew Thompson /* normal request */ 707a593f6b8SAndrew Thompson err = usbd_req_get_config_desc_full(udev, 7082c79a775SHans Petter Selasky NULL, &cdp, index); 7097efaaa9aSAndrew Thompson } 71002ac6454SAndrew Thompson if (err) { 71102ac6454SAndrew Thompson goto done; 71202ac6454SAndrew Thompson } 71302ac6454SAndrew Thompson /* set the new config descriptor */ 71402ac6454SAndrew Thompson 71502ac6454SAndrew Thompson udev->cdesc = cdp; 71602ac6454SAndrew Thompson 71702ac6454SAndrew Thompson /* Figure out if the device is self or bus powered. */ 71802ac6454SAndrew Thompson selfpowered = 0; 71902ac6454SAndrew Thompson if ((!udev->flags.uq_bus_powered) && 72002ac6454SAndrew Thompson (cdp->bmAttributes & UC_SELF_POWERED) && 721f29a0724SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST)) { 72202ac6454SAndrew Thompson /* May be self powered. */ 72302ac6454SAndrew Thompson if (cdp->bmAttributes & UC_BUS_POWERED) { 72402ac6454SAndrew Thompson /* Must ask device. */ 725a593f6b8SAndrew Thompson err = usbd_req_get_device_status(udev, NULL, &ds); 72602ac6454SAndrew Thompson if (err) { 72702ac6454SAndrew Thompson DPRINTFN(0, "could not read " 72802ac6454SAndrew Thompson "device status: %s\n", 729a593f6b8SAndrew Thompson usbd_errstr(err)); 73002ac6454SAndrew Thompson } else if (UGETW(ds.wStatus) & UDS_SELF_POWERED) { 73102ac6454SAndrew Thompson selfpowered = 1; 73202ac6454SAndrew Thompson } 73302ac6454SAndrew Thompson DPRINTF("status=0x%04x \n", 73402ac6454SAndrew Thompson UGETW(ds.wStatus)); 73502ac6454SAndrew Thompson } else 73602ac6454SAndrew Thompson selfpowered = 1; 73702ac6454SAndrew Thompson } 73802ac6454SAndrew Thompson DPRINTF("udev=%p cdesc=%p (addr %d) cno=%d attr=0x%02x, " 73902ac6454SAndrew Thompson "selfpowered=%d, power=%d\n", 74002ac6454SAndrew Thompson udev, cdp, 74191f72cedSAndrew Thompson udev->address, cdp->bConfigurationValue, cdp->bmAttributes, 74202ac6454SAndrew Thompson selfpowered, cdp->bMaxPower * 2); 74302ac6454SAndrew Thompson 74402ac6454SAndrew Thompson /* Check if we have enough power. */ 74502ac6454SAndrew Thompson power = cdp->bMaxPower * 2; 74602ac6454SAndrew Thompson 74702ac6454SAndrew Thompson if (udev->parent_hub) { 74802ac6454SAndrew Thompson max_power = udev->parent_hub->hub->portpower; 74902ac6454SAndrew Thompson } else { 75002ac6454SAndrew Thompson max_power = USB_MAX_POWER; 75102ac6454SAndrew Thompson } 75202ac6454SAndrew Thompson 75302ac6454SAndrew Thompson if (power > max_power) { 75402ac6454SAndrew Thompson DPRINTFN(0, "power exceeded %d > %d\n", power, max_power); 75502ac6454SAndrew Thompson err = USB_ERR_NO_POWER; 75602ac6454SAndrew Thompson goto done; 75702ac6454SAndrew Thompson } 75802ac6454SAndrew Thompson /* Only update "self_powered" in USB Host Mode */ 759f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 76002ac6454SAndrew Thompson udev->flags.self_powered = selfpowered; 76102ac6454SAndrew Thompson } 76202ac6454SAndrew Thompson udev->power = power; 76302ac6454SAndrew Thompson udev->curr_config_no = cdp->bConfigurationValue; 76402ac6454SAndrew Thompson udev->curr_config_index = index; 765a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_CONFIGURED); 76602ac6454SAndrew Thompson 76702ac6454SAndrew Thompson /* Set the actual configuration value. */ 768a593f6b8SAndrew Thompson err = usbd_req_set_config(udev, NULL, cdp->bConfigurationValue); 76902ac6454SAndrew Thompson if (err) { 77002ac6454SAndrew Thompson goto done; 77102ac6454SAndrew Thompson } 772bdd41206SAndrew Thompson 773a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_ALLOC); 77402ac6454SAndrew Thompson if (err) { 77502ac6454SAndrew Thompson goto done; 77602ac6454SAndrew Thompson } 777bdd41206SAndrew Thompson 778a593f6b8SAndrew Thompson err = usb_config_parse(udev, USB_IFACE_INDEX_ANY, USB_CFG_INIT); 779bdd41206SAndrew Thompson if (err) { 780bdd41206SAndrew Thompson goto done; 78102ac6454SAndrew Thompson } 782bdd41206SAndrew Thompson 7830f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 784ee3e3ff5SAndrew Thompson /* create device nodes for each endpoint */ 785a593f6b8SAndrew Thompson usb_cdev_create(udev); 7860f6e8f93SAndrew Thompson #endif 78702ac6454SAndrew Thompson 78802ac6454SAndrew Thompson done: 789a593f6b8SAndrew Thompson DPRINTF("error=%s\n", usbd_errstr(err)); 79002ac6454SAndrew Thompson if (err) { 791d88688c7SAndrew Thompson usb_unconfigure(udev, 0); 79202ac6454SAndrew Thompson } 793cb18f7d1SAlfred Perlstein if (do_unlock) 794cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 79502ac6454SAndrew Thompson return (err); 79602ac6454SAndrew Thompson } 79702ac6454SAndrew Thompson 79802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 799a593f6b8SAndrew Thompson * usb_config_parse 800bdd41206SAndrew Thompson * 801ae60fdfbSAndrew Thompson * This function will allocate and free USB interfaces and USB endpoints, 802ae60fdfbSAndrew Thompson * parse the USB configuration structure and initialise the USB endpoints 803bdd41206SAndrew Thompson * and interfaces. If "iface_index" is not equal to 804bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" then the "cmd" parameter is the 805bdd41206SAndrew Thompson * alternate_setting to be selected for the given interface. Else the 806bdd41206SAndrew Thompson * "cmd" parameter is defined by "USB_CFG_XXX". "iface_index" can be 807bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" or a valid USB interface index. This function 808bdd41206SAndrew Thompson * is typically called when setting the configuration or when setting 809bdd41206SAndrew Thompson * an alternate interface. 810bdd41206SAndrew Thompson * 811bdd41206SAndrew Thompson * Returns: 812bdd41206SAndrew Thompson * 0: Success 813bdd41206SAndrew Thompson * Else: Failure 814bdd41206SAndrew Thompson *------------------------------------------------------------------------*/ 815963169b4SHans Petter Selasky static usb_error_t 816a593f6b8SAndrew Thompson usb_config_parse(struct usb_device *udev, uint8_t iface_index, uint8_t cmd) 817bdd41206SAndrew Thompson { 818760bc48eSAndrew Thompson struct usb_idesc_parse_state ips; 819760bc48eSAndrew Thompson struct usb_interface_descriptor *id; 820760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 821760bc48eSAndrew Thompson struct usb_interface *iface; 822ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 823e0a69b51SAndrew Thompson usb_error_t err; 824bdd41206SAndrew Thompson uint8_t ep_curr; 825bdd41206SAndrew Thompson uint8_t ep_max; 826bdd41206SAndrew Thompson uint8_t temp; 827bdd41206SAndrew Thompson uint8_t do_init; 828bdd41206SAndrew Thompson uint8_t alt_index; 829bdd41206SAndrew Thompson 830bdd41206SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 831bdd41206SAndrew Thompson /* parameter overload */ 832bdd41206SAndrew Thompson alt_index = cmd; 833bdd41206SAndrew Thompson cmd = USB_CFG_INIT; 834bdd41206SAndrew Thompson } else { 835bdd41206SAndrew Thompson /* not used */ 836bdd41206SAndrew Thompson alt_index = 0; 837bdd41206SAndrew Thompson } 838bdd41206SAndrew Thompson 839bdd41206SAndrew Thompson err = 0; 840bdd41206SAndrew Thompson 841bdd41206SAndrew Thompson DPRINTFN(5, "iface_index=%d cmd=%d\n", 842bdd41206SAndrew Thompson iface_index, cmd); 843bdd41206SAndrew Thompson 844e4611d26SHans Petter Selasky if (cmd == USB_CFG_INIT || cmd == USB_CFG_FREE) { 84591cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 846bdd41206SAndrew Thompson 847ae60fdfbSAndrew Thompson /* check for in-use endpoints */ 848bdd41206SAndrew Thompson 849e4611d26SHans Petter Selasky if (cmd == USB_CFG_INIT) { 850e4611d26SHans Petter Selasky ep = udev->endpoints; 851e4611d26SHans Petter Selasky ep_max = udev->endpoints_max; 852e4611d26SHans Petter Selasky while (ep_max--) { 853e4611d26SHans Petter Selasky /* look for matching endpoints */ 854e4611d26SHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY || 855e4611d26SHans Petter Selasky iface_index == ep->iface_index) { 856e4611d26SHans Petter Selasky if (ep->refcount_alloc != 0) 857e4611d26SHans Petter Selasky return (USB_ERR_IN_USE); 858e4611d26SHans Petter Selasky } 859e4611d26SHans Petter Selasky } 860e4611d26SHans Petter Selasky } 861e4611d26SHans Petter Selasky 862ae60fdfbSAndrew Thompson ep = udev->endpoints; 863ae60fdfbSAndrew Thompson ep_max = udev->endpoints_max; 864bdd41206SAndrew Thompson while (ep_max--) { 865ae60fdfbSAndrew Thompson /* look for matching endpoints */ 866e4611d26SHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY || 867e4611d26SHans Petter Selasky iface_index == ep->iface_index) { 868bdd41206SAndrew Thompson /* 869e4611d26SHans Petter Selasky * Check if hardware needs a callback 870e4611d26SHans Petter Selasky * to unconfigure the endpoint. This 871e4611d26SHans Petter Selasky * may happen multiple times, 872e4611d26SHans Petter Selasky * because the requested alternate 873e4611d26SHans Petter Selasky * setting may fail. The callback 874e4611d26SHans Petter Selasky * implementation should be aware of 875e4611d26SHans Petter Selasky * and handle that. 876bdd41206SAndrew Thompson */ 877e4611d26SHans Petter Selasky if (ep->edesc != NULL && 878e4611d26SHans Petter Selasky udev->bus->methods->endpoint_uninit != NULL) 879e4611d26SHans Petter Selasky udev->bus->methods->endpoint_uninit(udev, ep); 880e4611d26SHans Petter Selasky 881ae60fdfbSAndrew Thompson /* reset endpoint */ 882ae60fdfbSAndrew Thompson memset(ep, 0, sizeof(*ep)); 883ae60fdfbSAndrew Thompson /* make sure we don't zero the endpoint again */ 884ae60fdfbSAndrew Thompson ep->iface_index = USB_IFACE_INDEX_ANY; 885bdd41206SAndrew Thompson } 886ae60fdfbSAndrew Thompson ep++; 887bdd41206SAndrew Thompson } 888bdd41206SAndrew Thompson 889e4611d26SHans Petter Selasky if (cmd == USB_CFG_FREE) 890e4611d26SHans Petter Selasky goto cleanup; 891bdd41206SAndrew Thompson } 892bdd41206SAndrew Thompson 893bdd41206SAndrew Thompson memset(&ips, 0, sizeof(ips)); 894bdd41206SAndrew Thompson 895bdd41206SAndrew Thompson ep_curr = 0; 896bdd41206SAndrew Thompson ep_max = 0; 897bdd41206SAndrew Thompson 898a593f6b8SAndrew Thompson while ((id = usb_idesc_foreach(udev->cdesc, &ips))) { 899bdd41206SAndrew Thompson iface = udev->ifaces + ips.iface_index; 900bdd41206SAndrew Thompson 901bdd41206SAndrew Thompson /* check for specific interface match */ 902bdd41206SAndrew Thompson 903bdd41206SAndrew Thompson if (cmd == USB_CFG_INIT) { 904bdd41206SAndrew Thompson if ((iface_index != USB_IFACE_INDEX_ANY) && 905bdd41206SAndrew Thompson (iface_index != ips.iface_index)) { 906bdd41206SAndrew Thompson /* wrong interface */ 907bdd41206SAndrew Thompson do_init = 0; 908bdd41206SAndrew Thompson } else if (alt_index != ips.iface_index_alt) { 909bdd41206SAndrew Thompson /* wrong alternate setting */ 910bdd41206SAndrew Thompson do_init = 0; 911bdd41206SAndrew Thompson } else { 912bdd41206SAndrew Thompson /* initialise interface */ 913bdd41206SAndrew Thompson do_init = 1; 914bdd41206SAndrew Thompson } 915b1f99f9cSHans Petter Selasky /* update number of alternate settings, if any */ 916b1f99f9cSHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) 917b1f99f9cSHans Petter Selasky iface->num_altsetting = ips.iface_index_alt + 1; 918bdd41206SAndrew Thompson } else 919bdd41206SAndrew Thompson do_init = 0; 920bdd41206SAndrew Thompson 921bdd41206SAndrew Thompson /* check for new interface */ 922bdd41206SAndrew Thompson if (ips.iface_index_alt == 0) { 923bdd41206SAndrew Thompson /* update current number of endpoints */ 924bdd41206SAndrew Thompson ep_curr = ep_max; 925bdd41206SAndrew Thompson } 926b8b3f4fdSHans Petter Selasky 927bdd41206SAndrew Thompson /* check for init */ 928bdd41206SAndrew Thompson if (do_init) { 929bdd41206SAndrew Thompson /* setup the USB interface structure */ 930bdd41206SAndrew Thompson iface->idesc = id; 931bdd41206SAndrew Thompson /* set alternate index */ 932bdd41206SAndrew Thompson iface->alt_index = alt_index; 933d9073c1eSHans Petter Selasky /* set default interface parent */ 934d9073c1eSHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 935d9073c1eSHans Petter Selasky iface->parent_iface_index = 936d9073c1eSHans Petter Selasky USB_IFACE_INDEX_ANY; 937d9073c1eSHans Petter Selasky } 938bdd41206SAndrew Thompson } 939bdd41206SAndrew Thompson 940bdd41206SAndrew Thompson DPRINTFN(5, "found idesc nendpt=%d\n", id->bNumEndpoints); 941bdd41206SAndrew Thompson 942760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)id; 943bdd41206SAndrew Thompson 944bdd41206SAndrew Thompson temp = ep_curr; 945bdd41206SAndrew Thompson 946bdd41206SAndrew Thompson /* iterate all the endpoint descriptors */ 947a593f6b8SAndrew Thompson while ((ed = usb_edesc_foreach(udev->cdesc, ed))) { 9485b0752bbSHans Petter Selasky /* check if endpoint limit has been reached */ 9495b0752bbSHans Petter Selasky if (temp >= USB_MAX_EP_UNITS) { 9505b0752bbSHans Petter Selasky DPRINTF("Endpoint limit reached\n"); 9515b0752bbSHans Petter Selasky break; 9525b0752bbSHans Petter Selasky } 953bdd41206SAndrew Thompson 954ae60fdfbSAndrew Thompson ep = udev->endpoints + temp; 955bdd41206SAndrew Thompson 956bdd41206SAndrew Thompson if (do_init) { 957963169b4SHans Petter Selasky void *ecomp; 958963169b4SHans Petter Selasky 959963169b4SHans Petter Selasky ecomp = usb_ed_comp_foreach(udev->cdesc, (void *)ed); 960963169b4SHans Petter Selasky if (ecomp != NULL) 961963169b4SHans Petter Selasky DPRINTFN(5, "Found endpoint companion descriptor\n"); 962963169b4SHans Petter Selasky 963a593f6b8SAndrew Thompson usb_init_endpoint(udev, 964963169b4SHans Petter Selasky ips.iface_index, ed, ecomp, ep); 965bdd41206SAndrew Thompson } 966bdd41206SAndrew Thompson 967bdd41206SAndrew Thompson temp ++; 968bdd41206SAndrew Thompson 969bdd41206SAndrew Thompson /* find maximum number of endpoints */ 970bdd41206SAndrew Thompson if (ep_max < temp) 971bdd41206SAndrew Thompson ep_max = temp; 972bdd41206SAndrew Thompson } 973bdd41206SAndrew Thompson } 974bdd41206SAndrew Thompson 975bdd41206SAndrew Thompson /* NOTE: It is valid to have no interfaces and no endpoints! */ 976bdd41206SAndrew Thompson 977bdd41206SAndrew Thompson if (cmd == USB_CFG_ALLOC) { 978bdd41206SAndrew Thompson udev->ifaces_max = ips.iface_index; 9795b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) 980bdd41206SAndrew Thompson udev->ifaces = NULL; 981bdd41206SAndrew Thompson if (udev->ifaces_max != 0) { 982bdd41206SAndrew Thompson udev->ifaces = malloc(sizeof(*iface) * udev->ifaces_max, 983bdd41206SAndrew Thompson M_USB, M_WAITOK | M_ZERO); 984bdd41206SAndrew Thompson if (udev->ifaces == NULL) { 985bdd41206SAndrew Thompson err = USB_ERR_NOMEM; 986bdd41206SAndrew Thompson goto done; 987bdd41206SAndrew Thompson } 988bdd41206SAndrew Thompson } 9895b0752bbSHans Petter Selasky #endif 9905b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0) 9919469c929SAndrew Thompson if (ep_max != 0) { 992ae60fdfbSAndrew Thompson udev->endpoints = malloc(sizeof(*ep) * ep_max, 993bdd41206SAndrew Thompson M_USB, M_WAITOK | M_ZERO); 994ae60fdfbSAndrew Thompson if (udev->endpoints == NULL) { 995bdd41206SAndrew Thompson err = USB_ERR_NOMEM; 996bdd41206SAndrew Thompson goto done; 997bdd41206SAndrew Thompson } 9989469c929SAndrew Thompson } else { 999ae60fdfbSAndrew Thompson udev->endpoints = NULL; 1000bdd41206SAndrew Thompson } 10015b0752bbSHans Petter Selasky #endif 10029469c929SAndrew Thompson USB_BUS_LOCK(udev->bus); 1003ae60fdfbSAndrew Thompson udev->endpoints_max = ep_max; 10049469c929SAndrew Thompson /* reset any ongoing clear-stall */ 1005ae60fdfbSAndrew Thompson udev->ep_curr = NULL; 10069469c929SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 1007bdd41206SAndrew Thompson } 10086c218933SHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) || (USB_HAVE_FIXED_ENDPOINT == 0) 1009bdd41206SAndrew Thompson done: 10106c218933SHans Petter Selasky #endif 1011bdd41206SAndrew Thompson if (err) { 1012bdd41206SAndrew Thompson if (cmd == USB_CFG_ALLOC) { 1013bdd41206SAndrew Thompson cleanup: 10149469c929SAndrew Thompson USB_BUS_LOCK(udev->bus); 1015ae60fdfbSAndrew Thompson udev->endpoints_max = 0; 10169469c929SAndrew Thompson /* reset any ongoing clear-stall */ 1017ae60fdfbSAndrew Thompson udev->ep_curr = NULL; 10189469c929SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 10199469c929SAndrew Thompson 10205b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_IFACE == 0) 1021bdd41206SAndrew Thompson free(udev->ifaces, M_USB); 1022bdd41206SAndrew Thompson udev->ifaces = NULL; 10235b0752bbSHans Petter Selasky #endif 10245b0752bbSHans Petter Selasky #if (USB_HAVE_FIXED_ENDPOINT == 0) 10255b0752bbSHans Petter Selasky free(udev->endpoints, M_USB); 1026ae60fdfbSAndrew Thompson udev->endpoints = NULL; 10275b0752bbSHans Petter Selasky #endif 1028bdd41206SAndrew Thompson udev->ifaces_max = 0; 1029bdd41206SAndrew Thompson } 1030bdd41206SAndrew Thompson } 1031bdd41206SAndrew Thompson return (err); 1032bdd41206SAndrew Thompson } 1033bdd41206SAndrew Thompson 1034bdd41206SAndrew Thompson /*------------------------------------------------------------------------* 1035a593f6b8SAndrew Thompson * usbd_set_alt_interface_index 103602ac6454SAndrew Thompson * 103702ac6454SAndrew Thompson * This function will select an alternate interface index for the 103802ac6454SAndrew Thompson * given interface index. The interface should not be in use when this 1039e2805033SAndrew Thompson * function is called. That means there should not be any open USB 1040e2805033SAndrew Thompson * transfers. Else an error is returned. If the alternate setting is 1041e2805033SAndrew Thompson * already set this function will simply return success. This function 1042e2805033SAndrew Thompson * is called in Host mode and Device mode! 104302ac6454SAndrew Thompson * 104402ac6454SAndrew Thompson * Returns: 104502ac6454SAndrew Thompson * 0: Success 104602ac6454SAndrew Thompson * Else: Failure 104702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1048e0a69b51SAndrew Thompson usb_error_t 1049a593f6b8SAndrew Thompson usbd_set_alt_interface_index(struct usb_device *udev, 105002ac6454SAndrew Thompson uint8_t iface_index, uint8_t alt_index) 105102ac6454SAndrew Thompson { 1052a593f6b8SAndrew Thompson struct usb_interface *iface = usbd_get_iface(udev, iface_index); 1053e0a69b51SAndrew Thompson usb_error_t err; 105402ac6454SAndrew Thompson uint8_t do_unlock; 105502ac6454SAndrew Thompson 10566950c75fSHans Petter Selasky /* Prevent re-enumeration */ 10576950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 10586950c75fSHans Petter Selasky 105902ac6454SAndrew Thompson if (iface == NULL) { 106002ac6454SAndrew Thompson err = USB_ERR_INVAL; 106102ac6454SAndrew Thompson goto done; 106202ac6454SAndrew Thompson } 1063e2805033SAndrew Thompson if (iface->alt_index == alt_index) { 1064e2805033SAndrew Thompson /* 1065e2805033SAndrew Thompson * Optimise away duplicate setting of 1066e2805033SAndrew Thompson * alternate setting in USB Host Mode! 1067e2805033SAndrew Thompson */ 1068e2805033SAndrew Thompson err = 0; 1069e2805033SAndrew Thompson goto done; 1070e2805033SAndrew Thompson } 10710f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 107202ac6454SAndrew Thompson /* 107302ac6454SAndrew Thompson * Free all generic FIFOs for this interface, except control 107402ac6454SAndrew Thompson * endpoint FIFOs: 107502ac6454SAndrew Thompson */ 1076a593f6b8SAndrew Thompson usb_fifo_free_wrap(udev, iface_index, 0); 10770f6e8f93SAndrew Thompson #endif 1078bdd41206SAndrew Thompson 1079a593f6b8SAndrew Thompson err = usb_config_parse(udev, iface_index, alt_index); 108002ac6454SAndrew Thompson if (err) { 108102ac6454SAndrew Thompson goto done; 108202ac6454SAndrew Thompson } 1083bd73b187SAlfred Perlstein if (iface->alt_index != alt_index) { 1084bd73b187SAlfred Perlstein /* the alternate setting does not exist */ 1085bd73b187SAlfred Perlstein err = USB_ERR_INVAL; 1086bd73b187SAlfred Perlstein goto done; 1087bd73b187SAlfred Perlstein } 1088bd73b187SAlfred Perlstein 1089a593f6b8SAndrew Thompson err = usbd_req_set_alt_interface_no(udev, NULL, iface_index, 109002ac6454SAndrew Thompson iface->idesc->bAlternateSetting); 109102ac6454SAndrew Thompson 109202ac6454SAndrew Thompson done: 1093cb18f7d1SAlfred Perlstein if (do_unlock) 1094cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 109502ac6454SAndrew Thompson return (err); 109602ac6454SAndrew Thompson } 109702ac6454SAndrew Thompson 109802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1099a593f6b8SAndrew Thompson * usbd_set_endpoint_stall 110002ac6454SAndrew Thompson * 1101963169b4SHans Petter Selasky * This function is used to make a BULK or INTERRUPT endpoint send 1102963169b4SHans Petter Selasky * STALL tokens in USB device mode. 110302ac6454SAndrew Thompson * 110402ac6454SAndrew Thompson * Returns: 110502ac6454SAndrew Thompson * 0: Success 110602ac6454SAndrew Thompson * Else: Failure 110702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1108e0a69b51SAndrew Thompson usb_error_t 1109a593f6b8SAndrew Thompson usbd_set_endpoint_stall(struct usb_device *udev, struct usb_endpoint *ep, 111002ac6454SAndrew Thompson uint8_t do_stall) 111102ac6454SAndrew Thompson { 1112760bc48eSAndrew Thompson struct usb_xfer *xfer; 1113a5cf1aaaSHans Petter Selasky usb_stream_t x; 111402ac6454SAndrew Thompson uint8_t et; 111502ac6454SAndrew Thompson uint8_t was_stalled; 111602ac6454SAndrew Thompson 1117ae60fdfbSAndrew Thompson if (ep == NULL) { 111802ac6454SAndrew Thompson /* nothing to do */ 111902ac6454SAndrew Thompson DPRINTF("Cannot find endpoint\n"); 112002ac6454SAndrew Thompson /* 112102ac6454SAndrew Thompson * Pretend that the clear or set stall request is 112202ac6454SAndrew Thompson * successful else some USB host stacks can do 112302ac6454SAndrew Thompson * strange things, especially when a control endpoint 112402ac6454SAndrew Thompson * stalls. 112502ac6454SAndrew Thompson */ 112602ac6454SAndrew Thompson return (0); 112702ac6454SAndrew Thompson } 1128ae60fdfbSAndrew Thompson et = (ep->edesc->bmAttributes & UE_XFERTYPE); 112902ac6454SAndrew Thompson 113002ac6454SAndrew Thompson if ((et != UE_BULK) && 113102ac6454SAndrew Thompson (et != UE_INTERRUPT)) { 113202ac6454SAndrew Thompson /* 113302ac6454SAndrew Thompson * Should not stall control 113402ac6454SAndrew Thompson * nor isochronous endpoints. 113502ac6454SAndrew Thompson */ 113602ac6454SAndrew Thompson DPRINTF("Invalid endpoint\n"); 113702ac6454SAndrew Thompson return (0); 113802ac6454SAndrew Thompson } 113902ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 114002ac6454SAndrew Thompson 114102ac6454SAndrew Thompson /* store current stall state */ 1142ae60fdfbSAndrew Thompson was_stalled = ep->is_stalled; 114302ac6454SAndrew Thompson 114402ac6454SAndrew Thompson /* check for no change */ 114502ac6454SAndrew Thompson if (was_stalled && do_stall) { 1146ae60fdfbSAndrew Thompson /* if the endpoint is already stalled do nothing */ 114702ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 114802ac6454SAndrew Thompson DPRINTF("No change\n"); 114902ac6454SAndrew Thompson return (0); 115002ac6454SAndrew Thompson } 115102ac6454SAndrew Thompson /* set stalled state */ 1152ae60fdfbSAndrew Thompson ep->is_stalled = 1; 115302ac6454SAndrew Thompson 115402ac6454SAndrew Thompson if (do_stall || (!was_stalled)) { 115502ac6454SAndrew Thompson if (!was_stalled) { 1156a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 115702ac6454SAndrew Thompson /* lookup the current USB transfer, if any */ 1158a5cf1aaaSHans Petter Selasky xfer = ep->endpoint_q[x].curr; 1159a5cf1aaaSHans Petter Selasky if (xfer != NULL) { 116002ac6454SAndrew Thompson /* 1161a5cf1aaaSHans Petter Selasky * The "xfer_stall" method 1162a5cf1aaaSHans Petter Selasky * will complete the USB 1163a5cf1aaaSHans Petter Selasky * transfer like in case of a 1164a5cf1aaaSHans Petter Selasky * timeout setting the error 1165a5cf1aaaSHans Petter Selasky * code "USB_ERR_STALLED". 116602ac6454SAndrew Thompson */ 1167a5cf1aaaSHans Petter Selasky (udev->bus->methods->xfer_stall) (xfer); 1168a5cf1aaaSHans Petter Selasky } 1169a5cf1aaaSHans Petter Selasky } 1170a5cf1aaaSHans Petter Selasky } 1171a5cf1aaaSHans Petter Selasky (udev->bus->methods->set_stall) (udev, ep, &do_stall); 117202ac6454SAndrew Thompson } 117302ac6454SAndrew Thompson if (!do_stall) { 1174ae60fdfbSAndrew Thompson ep->toggle_next = 0; /* reset data toggle */ 1175ae60fdfbSAndrew Thompson ep->is_stalled = 0; /* clear stalled state */ 117602ac6454SAndrew Thompson 1177ae60fdfbSAndrew Thompson (udev->bus->methods->clear_stall) (udev, ep); 117802ac6454SAndrew Thompson 1179a5cf1aaaSHans Petter Selasky /* start the current or next transfer, if any */ 1180a5cf1aaaSHans Petter Selasky for (x = 0; x != USB_MAX_EP_STREAMS; x++) { 1181a5cf1aaaSHans Petter Selasky usb_command_wrapper(&ep->endpoint_q[x], 1182a5cf1aaaSHans Petter Selasky ep->endpoint_q[x].curr); 1183a5cf1aaaSHans Petter Selasky } 118402ac6454SAndrew Thompson } 118502ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 118602ac6454SAndrew Thompson return (0); 118702ac6454SAndrew Thompson } 118802ac6454SAndrew Thompson 118902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1190a593f6b8SAndrew Thompson * usb_reset_iface_endpoints - used in USB device side mode 119102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1192e0a69b51SAndrew Thompson usb_error_t 1193a593f6b8SAndrew Thompson usb_reset_iface_endpoints(struct usb_device *udev, uint8_t iface_index) 119402ac6454SAndrew Thompson { 1195ae60fdfbSAndrew Thompson struct usb_endpoint *ep; 1196ae60fdfbSAndrew Thompson struct usb_endpoint *ep_end; 119702ac6454SAndrew Thompson 1198ae60fdfbSAndrew Thompson ep = udev->endpoints; 1199ae60fdfbSAndrew Thompson ep_end = udev->endpoints + udev->endpoints_max; 120002ac6454SAndrew Thompson 1201ae60fdfbSAndrew Thompson for (; ep != ep_end; ep++) { 1202ae60fdfbSAndrew Thompson if ((ep->edesc == NULL) || 1203ae60fdfbSAndrew Thompson (ep->iface_index != iface_index)) { 120402ac6454SAndrew Thompson continue; 120502ac6454SAndrew Thompson } 120602ac6454SAndrew Thompson /* simulate a clear stall from the peer */ 1207bd73b187SAlfred Perlstein usbd_set_endpoint_stall(udev, ep, 0); 120802ac6454SAndrew Thompson } 120902ac6454SAndrew Thompson return (0); 121002ac6454SAndrew Thompson } 121102ac6454SAndrew Thompson 121202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1213a593f6b8SAndrew Thompson * usb_detach_device_sub 121402ac6454SAndrew Thompson * 121502ac6454SAndrew Thompson * This function will try to detach an USB device. If it fails a panic 121602ac6454SAndrew Thompson * will result. 1217bdd41206SAndrew Thompson * 1218bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 121902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 122002ac6454SAndrew Thompson static void 1221a593f6b8SAndrew Thompson usb_detach_device_sub(struct usb_device *udev, device_t *ppdev, 12228427ed84SHans Petter Selasky char **ppnpinfo, uint8_t flag) 122302ac6454SAndrew Thompson { 122402ac6454SAndrew Thompson device_t dev; 12258427ed84SHans Petter Selasky char *pnpinfo; 122602ac6454SAndrew Thompson int err; 122702ac6454SAndrew Thompson 1228d88688c7SAndrew Thompson dev = *ppdev; 1229d88688c7SAndrew Thompson if (dev) { 123002ac6454SAndrew Thompson /* 123102ac6454SAndrew Thompson * NOTE: It is important to clear "*ppdev" before deleting 123202ac6454SAndrew Thompson * the child due to some device methods being called late 123302ac6454SAndrew Thompson * during the delete process ! 123402ac6454SAndrew Thompson */ 123502ac6454SAndrew Thompson *ppdev = NULL; 123602ac6454SAndrew Thompson 1237cabe79d9SMarius Strobl if (!rebooting) { 123802ac6454SAndrew Thompson device_printf(dev, "at %s, port %d, addr %d " 123902ac6454SAndrew Thompson "(disconnected)\n", 124002ac6454SAndrew Thompson device_get_nameunit(udev->parent_dev), 124102ac6454SAndrew Thompson udev->port_no, udev->address); 1242cabe79d9SMarius Strobl } 124302ac6454SAndrew Thompson 124402ac6454SAndrew Thompson if (device_is_attached(dev)) { 1245ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 124602ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 124702ac6454SAndrew Thompson if (err) { 1248767cb2e2SAndrew Thompson device_printf(dev, "Resume failed\n"); 124902ac6454SAndrew Thompson } 125002ac6454SAndrew Thompson } 125102ac6454SAndrew Thompson } 1252d3bf5efcSHans Petter Selasky /* detach and delete child */ 125302ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 125402ac6454SAndrew Thompson goto error; 125502ac6454SAndrew Thompson } 125602ac6454SAndrew Thompson } 12578427ed84SHans Petter Selasky 12588427ed84SHans Petter Selasky pnpinfo = *ppnpinfo; 12598427ed84SHans Petter Selasky if (pnpinfo != NULL) { 12608427ed84SHans Petter Selasky *ppnpinfo = NULL; 12618427ed84SHans Petter Selasky free(pnpinfo, M_USBDEV); 12628427ed84SHans Petter Selasky } 126302ac6454SAndrew Thompson return; 126402ac6454SAndrew Thompson 126502ac6454SAndrew Thompson error: 126602ac6454SAndrew Thompson /* Detach is not allowed to fail in the USB world */ 12678427ed84SHans Petter Selasky panic("usb_detach_device_sub: A USB driver would not detach\n"); 126802ac6454SAndrew Thompson } 126902ac6454SAndrew Thompson 127002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1271a593f6b8SAndrew Thompson * usb_detach_device 127202ac6454SAndrew Thompson * 127302ac6454SAndrew Thompson * The following function will detach the matching interfaces. 127402ac6454SAndrew Thompson * This function is NULL safe. 1275bdd41206SAndrew Thompson * 1276bdd41206SAndrew Thompson * Flag values, see "USB_UNCFG_FLAG_XXX". 127702ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 127802ac6454SAndrew Thompson void 1279a593f6b8SAndrew Thompson usb_detach_device(struct usb_device *udev, uint8_t iface_index, 1280bdd41206SAndrew Thompson uint8_t flag) 128102ac6454SAndrew Thompson { 1282760bc48eSAndrew Thompson struct usb_interface *iface; 128302ac6454SAndrew Thompson uint8_t i; 128402ac6454SAndrew Thompson 128502ac6454SAndrew Thompson if (udev == NULL) { 128602ac6454SAndrew Thompson /* nothing to do */ 128702ac6454SAndrew Thompson return; 128802ac6454SAndrew Thompson } 128902ac6454SAndrew Thompson DPRINTFN(4, "udev=%p\n", udev); 129002ac6454SAndrew Thompson 129191cd9240SAndrew Thompson sx_assert(&udev->enum_sx, SA_LOCKED); 129202ac6454SAndrew Thompson 129302ac6454SAndrew Thompson /* 129402ac6454SAndrew Thompson * First detach the child to give the child's detach routine a 129502ac6454SAndrew Thompson * chance to detach the sub-devices in the correct order. 129602ac6454SAndrew Thompson * Then delete the child using "device_delete_child()" which 129702ac6454SAndrew Thompson * will detach all sub-devices from the bottom and upwards! 129802ac6454SAndrew Thompson */ 129902ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 130002ac6454SAndrew Thompson i = iface_index; 130102ac6454SAndrew Thompson iface_index = i + 1; 130202ac6454SAndrew Thompson } else { 130302ac6454SAndrew Thompson i = 0; 130402ac6454SAndrew Thompson iface_index = USB_IFACE_MAX; 130502ac6454SAndrew Thompson } 130602ac6454SAndrew Thompson 130702ac6454SAndrew Thompson /* do the detach */ 130802ac6454SAndrew Thompson 130902ac6454SAndrew Thompson for (; i != iface_index; i++) { 1310a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 131102ac6454SAndrew Thompson if (iface == NULL) { 131202ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 131302ac6454SAndrew Thompson break; 131402ac6454SAndrew Thompson } 13158427ed84SHans Petter Selasky usb_detach_device_sub(udev, &iface->subdev, 13168427ed84SHans Petter Selasky &iface->pnpinfo, flag); 131702ac6454SAndrew Thompson } 131802ac6454SAndrew Thompson } 131902ac6454SAndrew Thompson 132002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1321a593f6b8SAndrew Thompson * usb_probe_and_attach_sub 132202ac6454SAndrew Thompson * 132302ac6454SAndrew Thompson * Returns: 132402ac6454SAndrew Thompson * 0: Success 132502ac6454SAndrew Thompson * Else: Failure 132602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 132702ac6454SAndrew Thompson static uint8_t 1328a593f6b8SAndrew Thompson usb_probe_and_attach_sub(struct usb_device *udev, 1329760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 133002ac6454SAndrew Thompson { 1331760bc48eSAndrew Thompson struct usb_interface *iface; 133202ac6454SAndrew Thompson device_t dev; 133302ac6454SAndrew Thompson int err; 133402ac6454SAndrew Thompson 133502ac6454SAndrew Thompson iface = uaa->iface; 133602ac6454SAndrew Thompson if (iface->parent_iface_index != USB_IFACE_INDEX_ANY) { 133702ac6454SAndrew Thompson /* leave interface alone */ 133802ac6454SAndrew Thompson return (0); 133902ac6454SAndrew Thompson } 134002ac6454SAndrew Thompson dev = iface->subdev; 134102ac6454SAndrew Thompson if (dev) { 134202ac6454SAndrew Thompson /* clean up after module unload */ 134302ac6454SAndrew Thompson 134402ac6454SAndrew Thompson if (device_is_attached(dev)) { 134502ac6454SAndrew Thompson /* already a device there */ 134602ac6454SAndrew Thompson return (0); 134702ac6454SAndrew Thompson } 134802ac6454SAndrew Thompson /* clear "iface->subdev" as early as possible */ 134902ac6454SAndrew Thompson 135002ac6454SAndrew Thompson iface->subdev = NULL; 135102ac6454SAndrew Thompson 135202ac6454SAndrew Thompson if (device_delete_child(udev->parent_dev, dev)) { 135302ac6454SAndrew Thompson /* 135402ac6454SAndrew Thompson * Panic here, else one can get a double call 135502ac6454SAndrew Thompson * to device_detach(). USB devices should 135602ac6454SAndrew Thompson * never fail on detach! 135702ac6454SAndrew Thompson */ 1358767cb2e2SAndrew Thompson panic("device_delete_child() failed\n"); 135902ac6454SAndrew Thompson } 136002ac6454SAndrew Thompson } 136102ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 136202ac6454SAndrew Thompson /* create a new child */ 136302ac6454SAndrew Thompson uaa->temp_dev = device_add_child(udev->parent_dev, NULL, -1); 136402ac6454SAndrew Thompson if (uaa->temp_dev == NULL) { 136502ac6454SAndrew Thompson device_printf(udev->parent_dev, 1366767cb2e2SAndrew Thompson "Device creation failed\n"); 136702ac6454SAndrew Thompson return (1); /* failure */ 136802ac6454SAndrew Thompson } 136902ac6454SAndrew Thompson device_set_ivars(uaa->temp_dev, uaa); 137002ac6454SAndrew Thompson device_quiet(uaa->temp_dev); 137102ac6454SAndrew Thompson } 137202ac6454SAndrew Thompson /* 137302ac6454SAndrew Thompson * Set "subdev" before probe and attach so that "devd" gets 137402ac6454SAndrew Thompson * the information it needs. 137502ac6454SAndrew Thompson */ 137602ac6454SAndrew Thompson iface->subdev = uaa->temp_dev; 137702ac6454SAndrew Thompson 137802ac6454SAndrew Thompson if (device_probe_and_attach(iface->subdev) == 0) { 137902ac6454SAndrew Thompson /* 138002ac6454SAndrew Thompson * The USB attach arguments are only available during probe 138102ac6454SAndrew Thompson * and attach ! 138202ac6454SAndrew Thompson */ 138302ac6454SAndrew Thompson uaa->temp_dev = NULL; 138402ac6454SAndrew Thompson device_set_ivars(iface->subdev, NULL); 138502ac6454SAndrew Thompson 1386ec8f3127SAndrew Thompson if (udev->flags.peer_suspended) { 138702ac6454SAndrew Thompson err = DEVICE_SUSPEND(iface->subdev); 138888b4e0abSAndrew Thompson if (err) 138902ac6454SAndrew Thompson device_printf(iface->subdev, "Suspend failed\n"); 139002ac6454SAndrew Thompson } 139102ac6454SAndrew Thompson return (0); /* success */ 139202ac6454SAndrew Thompson } else { 139302ac6454SAndrew Thompson /* No USB driver found */ 139402ac6454SAndrew Thompson iface->subdev = NULL; 139502ac6454SAndrew Thompson } 139602ac6454SAndrew Thompson return (1); /* failure */ 139702ac6454SAndrew Thompson } 139802ac6454SAndrew Thompson 139902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1400a593f6b8SAndrew Thompson * usbd_set_parent_iface 140102ac6454SAndrew Thompson * 140202ac6454SAndrew Thompson * Using this function will lock the alternate interface setting on an 140302ac6454SAndrew Thompson * interface. It is typically used for multi interface drivers. In USB 140402ac6454SAndrew Thompson * device side mode it is assumed that the alternate interfaces all 140502ac6454SAndrew Thompson * have the same endpoint descriptors. The default parent index value 140602ac6454SAndrew Thompson * is "USB_IFACE_INDEX_ANY". Then the alternate setting value is not 140702ac6454SAndrew Thompson * locked. 140802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 140902ac6454SAndrew Thompson void 1410a593f6b8SAndrew Thompson usbd_set_parent_iface(struct usb_device *udev, uint8_t iface_index, 141102ac6454SAndrew Thompson uint8_t parent_index) 141202ac6454SAndrew Thompson { 1413760bc48eSAndrew Thompson struct usb_interface *iface; 141402ac6454SAndrew Thompson 14155e055201SHans Petter Selasky if (udev == NULL || iface_index == parent_index) { 1416d9073c1eSHans Petter Selasky /* nothing to do */ 1417d9073c1eSHans Petter Selasky return; 141802ac6454SAndrew Thompson } 1419d9073c1eSHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 1420d9073c1eSHans Petter Selasky if (iface != NULL) 1421d9073c1eSHans Petter Selasky iface->parent_iface_index = parent_index; 142202ac6454SAndrew Thompson } 142302ac6454SAndrew Thompson 142402ac6454SAndrew Thompson static void 1425a593f6b8SAndrew Thompson usb_init_attach_arg(struct usb_device *udev, 1426760bc48eSAndrew Thompson struct usb_attach_arg *uaa) 142702ac6454SAndrew Thompson { 1428d46dc4adSHans Petter Selasky memset(uaa, 0, sizeof(*uaa)); 142902ac6454SAndrew Thompson 143002ac6454SAndrew Thompson uaa->device = udev; 1431f29a0724SAndrew Thompson uaa->usb_mode = udev->flags.usb_mode; 143202ac6454SAndrew Thompson uaa->port = udev->port_no; 14332a4c6157SAndrew Thompson uaa->dev_state = UAA_DEV_READY; 143402ac6454SAndrew Thompson 143502ac6454SAndrew Thompson uaa->info.idVendor = UGETW(udev->ddesc.idVendor); 143602ac6454SAndrew Thompson uaa->info.idProduct = UGETW(udev->ddesc.idProduct); 143702ac6454SAndrew Thompson uaa->info.bcdDevice = UGETW(udev->ddesc.bcdDevice); 143802ac6454SAndrew Thompson uaa->info.bDeviceClass = udev->ddesc.bDeviceClass; 143902ac6454SAndrew Thompson uaa->info.bDeviceSubClass = udev->ddesc.bDeviceSubClass; 144002ac6454SAndrew Thompson uaa->info.bDeviceProtocol = udev->ddesc.bDeviceProtocol; 144102ac6454SAndrew Thompson uaa->info.bConfigIndex = udev->curr_config_index; 144202ac6454SAndrew Thompson uaa->info.bConfigNum = udev->curr_config_no; 144302ac6454SAndrew Thompson } 144402ac6454SAndrew Thompson 144502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1446a593f6b8SAndrew Thompson * usb_probe_and_attach 144702ac6454SAndrew Thompson * 144802ac6454SAndrew Thompson * This function is called from "uhub_explore_sub()", 1449a593f6b8SAndrew Thompson * "usb_handle_set_config()" and "usb_handle_request()". 145002ac6454SAndrew Thompson * 145102ac6454SAndrew Thompson * Returns: 145202ac6454SAndrew Thompson * 0: Success 145302ac6454SAndrew Thompson * Else: A control transfer failed 145402ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1455e0a69b51SAndrew Thompson usb_error_t 1456a593f6b8SAndrew Thompson usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index) 145702ac6454SAndrew Thompson { 1458760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1459760bc48eSAndrew Thompson struct usb_interface *iface; 146002ac6454SAndrew Thompson uint8_t i; 146102ac6454SAndrew Thompson uint8_t j; 146202ac6454SAndrew Thompson uint8_t do_unlock; 146302ac6454SAndrew Thompson 146402ac6454SAndrew Thompson if (udev == NULL) { 146502ac6454SAndrew Thompson DPRINTF("udev == NULL\n"); 146602ac6454SAndrew Thompson return (USB_ERR_INVAL); 146702ac6454SAndrew Thompson } 14686950c75fSHans Petter Selasky /* Prevent re-enumeration */ 14696950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 147002ac6454SAndrew Thompson 147102ac6454SAndrew Thompson if (udev->curr_config_index == USB_UNCONFIG_INDEX) { 147202ac6454SAndrew Thompson /* do nothing - no configuration has been set */ 147302ac6454SAndrew Thompson goto done; 147402ac6454SAndrew Thompson } 147502ac6454SAndrew Thompson /* setup USB attach arguments */ 147602ac6454SAndrew Thompson 1477a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 147802ac6454SAndrew Thompson 1479a6b60150SHans Petter Selasky /* 1480a6b60150SHans Petter Selasky * If the whole USB device is targeted, invoke the USB event 1481a6b60150SHans Petter Selasky * handler(s): 1482a6b60150SHans Petter Selasky */ 1483a6b60150SHans Petter Selasky if (iface_index == USB_IFACE_INDEX_ANY) { 1484415bcd89SHans Petter Selasky if (usb_test_quirk(&uaa, UQ_MSC_DYMO_EJECT) != 0 && 1485415bcd89SHans Petter Selasky usb_dymo_eject(udev, 0) == 0) { 1486415bcd89SHans Petter Selasky /* success, mark the udev as disappearing */ 1487415bcd89SHans Petter Selasky uaa.dev_state = UAA_DEV_EJECTING; 1488415bcd89SHans Petter Selasky } 1489415bcd89SHans Petter Selasky 1490a6b60150SHans Petter Selasky EVENTHANDLER_INVOKE(usb_dev_configured, udev, &uaa); 1491a6b60150SHans Petter Selasky 1492a6b60150SHans Petter Selasky if (uaa.dev_state != UAA_DEV_READY) { 1493a6b60150SHans Petter Selasky /* leave device unconfigured */ 1494a6b60150SHans Petter Selasky usb_unconfigure(udev, 0); 1495a6b60150SHans Petter Selasky goto done; 1496a6b60150SHans Petter Selasky } 1497a6b60150SHans Petter Selasky } 1498a6b60150SHans Petter Selasky 149902ac6454SAndrew Thompson /* Check if only one interface should be probed: */ 150002ac6454SAndrew Thompson if (iface_index != USB_IFACE_INDEX_ANY) { 150102ac6454SAndrew Thompson i = iface_index; 150202ac6454SAndrew Thompson j = i + 1; 150302ac6454SAndrew Thompson } else { 150402ac6454SAndrew Thompson i = 0; 150502ac6454SAndrew Thompson j = USB_IFACE_MAX; 150602ac6454SAndrew Thompson } 150702ac6454SAndrew Thompson 150802ac6454SAndrew Thompson /* Do the probe and attach */ 150902ac6454SAndrew Thompson for (; i != j; i++) { 1510a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 151102ac6454SAndrew Thompson if (iface == NULL) { 151202ac6454SAndrew Thompson /* 151302ac6454SAndrew Thompson * Looks like the end of the USB 151402ac6454SAndrew Thompson * interfaces ! 151502ac6454SAndrew Thompson */ 151602ac6454SAndrew Thompson DPRINTFN(2, "end of interfaces " 151702ac6454SAndrew Thompson "at %u\n", i); 151802ac6454SAndrew Thompson break; 151902ac6454SAndrew Thompson } 152002ac6454SAndrew Thompson if (iface->idesc == NULL) { 152102ac6454SAndrew Thompson /* no interface descriptor */ 152202ac6454SAndrew Thompson continue; 152302ac6454SAndrew Thompson } 152402ac6454SAndrew Thompson uaa.iface = iface; 152502ac6454SAndrew Thompson 152602ac6454SAndrew Thompson uaa.info.bInterfaceClass = 152702ac6454SAndrew Thompson iface->idesc->bInterfaceClass; 152802ac6454SAndrew Thompson uaa.info.bInterfaceSubClass = 152902ac6454SAndrew Thompson iface->idesc->bInterfaceSubClass; 153002ac6454SAndrew Thompson uaa.info.bInterfaceProtocol = 153102ac6454SAndrew Thompson iface->idesc->bInterfaceProtocol; 153202ac6454SAndrew Thompson uaa.info.bIfaceIndex = i; 153302ac6454SAndrew Thompson uaa.info.bIfaceNum = 153402ac6454SAndrew Thompson iface->idesc->bInterfaceNumber; 1535bd73b187SAlfred Perlstein uaa.driver_info = 0; /* reset driver_info */ 153602ac6454SAndrew Thompson 153702ac6454SAndrew Thompson DPRINTFN(2, "iclass=%u/%u/%u iindex=%u/%u\n", 153802ac6454SAndrew Thompson uaa.info.bInterfaceClass, 153902ac6454SAndrew Thompson uaa.info.bInterfaceSubClass, 154002ac6454SAndrew Thompson uaa.info.bInterfaceProtocol, 154102ac6454SAndrew Thompson uaa.info.bIfaceIndex, 154202ac6454SAndrew Thompson uaa.info.bIfaceNum); 154302ac6454SAndrew Thompson 1544d43ffe94SHans Petter Selasky usb_probe_and_attach_sub(udev, &uaa); 154502ac6454SAndrew Thompson 1546d43ffe94SHans Petter Selasky /* 1547d43ffe94SHans Petter Selasky * Remove the leftover child, if any, to enforce that 1548d43ffe94SHans Petter Selasky * a new nomatch devd event is generated for the next 1549d43ffe94SHans Petter Selasky * interface if no driver is found: 1550d43ffe94SHans Petter Selasky */ 1551d43ffe94SHans Petter Selasky if (uaa.temp_dev == NULL) 1552d43ffe94SHans Petter Selasky continue; 1553d43ffe94SHans Petter Selasky if (device_delete_child(udev->parent_dev, uaa.temp_dev)) 1554767cb2e2SAndrew Thompson DPRINTFN(0, "device delete child failed\n"); 1555d43ffe94SHans Petter Selasky uaa.temp_dev = NULL; 155602ac6454SAndrew Thompson } 155702ac6454SAndrew Thompson done: 1558cb18f7d1SAlfred Perlstein if (do_unlock) 1559cb18f7d1SAlfred Perlstein usbd_enum_unlock(udev); 156002ac6454SAndrew Thompson return (0); 156102ac6454SAndrew Thompson } 156202ac6454SAndrew Thompson 156302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1564a593f6b8SAndrew Thompson * usb_suspend_resume_sub 156502ac6454SAndrew Thompson * 156602ac6454SAndrew Thompson * This function is called when the suspend or resume methods should 156702ac6454SAndrew Thompson * be executed on an USB device. 156802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 156902ac6454SAndrew Thompson static void 1570a593f6b8SAndrew Thompson usb_suspend_resume_sub(struct usb_device *udev, device_t dev, uint8_t do_suspend) 157102ac6454SAndrew Thompson { 157202ac6454SAndrew Thompson int err; 157302ac6454SAndrew Thompson 157402ac6454SAndrew Thompson if (dev == NULL) { 157502ac6454SAndrew Thompson return; 157602ac6454SAndrew Thompson } 157702ac6454SAndrew Thompson if (!device_is_attached(dev)) { 157802ac6454SAndrew Thompson return; 157902ac6454SAndrew Thompson } 158002ac6454SAndrew Thompson if (do_suspend) { 158102ac6454SAndrew Thompson err = DEVICE_SUSPEND(dev); 158202ac6454SAndrew Thompson } else { 158302ac6454SAndrew Thompson err = DEVICE_RESUME(dev); 158402ac6454SAndrew Thompson } 158502ac6454SAndrew Thompson if (err) { 1586767cb2e2SAndrew Thompson device_printf(dev, "%s failed\n", 158702ac6454SAndrew Thompson do_suspend ? "Suspend" : "Resume"); 158802ac6454SAndrew Thompson } 158902ac6454SAndrew Thompson } 159002ac6454SAndrew Thompson 159102ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1592a593f6b8SAndrew Thompson * usb_suspend_resume 159302ac6454SAndrew Thompson * 159402ac6454SAndrew Thompson * The following function will suspend or resume the USB device. 159502ac6454SAndrew Thompson * 159602ac6454SAndrew Thompson * Returns: 159702ac6454SAndrew Thompson * 0: Success 159802ac6454SAndrew Thompson * Else: Failure 159902ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1600e0a69b51SAndrew Thompson usb_error_t 1601a593f6b8SAndrew Thompson usb_suspend_resume(struct usb_device *udev, uint8_t do_suspend) 160202ac6454SAndrew Thompson { 1603760bc48eSAndrew Thompson struct usb_interface *iface; 160402ac6454SAndrew Thompson uint8_t i; 160502ac6454SAndrew Thompson 160602ac6454SAndrew Thompson if (udev == NULL) { 160702ac6454SAndrew Thompson /* nothing to do */ 160802ac6454SAndrew Thompson return (0); 160902ac6454SAndrew Thompson } 161002ac6454SAndrew Thompson DPRINTFN(4, "udev=%p do_suspend=%d\n", udev, do_suspend); 161102ac6454SAndrew Thompson 16122df1e9a6SAndrew Thompson sx_assert(&udev->sr_sx, SA_LOCKED); 161302ac6454SAndrew Thompson 161402ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 161502ac6454SAndrew Thompson /* filter the suspend events */ 1616ec8f3127SAndrew Thompson if (udev->flags.peer_suspended == do_suspend) { 161702ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 161802ac6454SAndrew Thompson /* nothing to do */ 161902ac6454SAndrew Thompson return (0); 162002ac6454SAndrew Thompson } 1621ec8f3127SAndrew Thompson udev->flags.peer_suspended = do_suspend; 162202ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 162302ac6454SAndrew Thompson 162402ac6454SAndrew Thompson /* do the suspend or resume */ 162502ac6454SAndrew Thompson 162602ac6454SAndrew Thompson for (i = 0; i != USB_IFACE_MAX; i++) { 1627a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, i); 162802ac6454SAndrew Thompson if (iface == NULL) { 162902ac6454SAndrew Thompson /* looks like the end of the USB interfaces */ 163002ac6454SAndrew Thompson break; 163102ac6454SAndrew Thompson } 1632a593f6b8SAndrew Thompson usb_suspend_resume_sub(udev, iface->subdev, do_suspend); 163302ac6454SAndrew Thompson } 163402ac6454SAndrew Thompson return (0); 163502ac6454SAndrew Thompson } 163602ac6454SAndrew Thompson 163702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1638a593f6b8SAndrew Thompson * usbd_clear_stall_proc 163902ac6454SAndrew Thompson * 164002ac6454SAndrew Thompson * This function performs generic USB clear stall operations. 164102ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 164202ac6454SAndrew Thompson static void 1643a593f6b8SAndrew Thompson usbd_clear_stall_proc(struct usb_proc_msg *_pm) 164402ac6454SAndrew Thompson { 1645a0d53e0bSHans Petter Selasky struct usb_udev_msg *pm = (void *)_pm; 1646760bc48eSAndrew Thompson struct usb_device *udev = pm->udev; 164702ac6454SAndrew Thompson 164802ac6454SAndrew Thompson /* Change lock */ 164902ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 16500eb8d462SHans Petter Selasky USB_MTX_LOCK(&udev->device_mtx); 165102ac6454SAndrew Thompson 165202ac6454SAndrew Thompson /* Start clear stall callback */ 16535b3bb704SAndrew Thompson usbd_transfer_start(udev->ctrl_xfer[1]); 165402ac6454SAndrew Thompson 165502ac6454SAndrew Thompson /* Change lock */ 16560eb8d462SHans Petter Selasky USB_MTX_UNLOCK(&udev->device_mtx); 165702ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 165802ac6454SAndrew Thompson } 165902ac6454SAndrew Thompson 166002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 1661f54ab96dSHans Petter Selasky * usb_get_langid 1662f54ab96dSHans Petter Selasky * 1663f54ab96dSHans Petter Selasky * This function tries to figure out the USB string language to use. 1664f54ab96dSHans Petter Selasky *------------------------------------------------------------------------*/ 1665f54ab96dSHans Petter Selasky void 1666f54ab96dSHans Petter Selasky usb_get_langid(struct usb_device *udev) 1667f54ab96dSHans Petter Selasky { 1668f54ab96dSHans Petter Selasky uint8_t *scratch_ptr; 1669f54ab96dSHans Petter Selasky uint8_t do_unlock; 1670f54ab96dSHans Petter Selasky int err; 1671f54ab96dSHans Petter Selasky 1672f54ab96dSHans Petter Selasky /* 1673f54ab96dSHans Petter Selasky * Workaround for buggy USB devices. 1674f54ab96dSHans Petter Selasky * 1675f54ab96dSHans Petter Selasky * It appears that some string-less USB chips will crash and 1676f54ab96dSHans Petter Selasky * disappear if any attempts are made to read any string 1677f54ab96dSHans Petter Selasky * descriptors. 1678f54ab96dSHans Petter Selasky * 1679f54ab96dSHans Petter Selasky * Try to detect such chips by checking the strings in the USB 1680f54ab96dSHans Petter Selasky * device descriptor. If no strings are present there we 1681f54ab96dSHans Petter Selasky * simply disable all USB strings. 1682f54ab96dSHans Petter Selasky */ 1683f54ab96dSHans Petter Selasky 1684f54ab96dSHans Petter Selasky /* Protect scratch area */ 1685f54ab96dSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 1686f54ab96dSHans Petter Selasky 1687f54ab96dSHans Petter Selasky scratch_ptr = udev->scratch.data; 1688f54ab96dSHans Petter Selasky 1689f54ab96dSHans Petter Selasky if (udev->flags.no_strings) { 1690f54ab96dSHans Petter Selasky err = USB_ERR_INVAL; 1691f54ab96dSHans Petter Selasky } else if (udev->ddesc.iManufacturer || 1692f54ab96dSHans Petter Selasky udev->ddesc.iProduct || 1693f54ab96dSHans Petter Selasky udev->ddesc.iSerialNumber) { 1694f54ab96dSHans Petter Selasky /* read out the language ID string */ 1695f54ab96dSHans Petter Selasky err = usbd_req_get_string_desc(udev, NULL, 1696f54ab96dSHans Petter Selasky (char *)scratch_ptr, 4, 0, USB_LANGUAGE_TABLE); 1697f54ab96dSHans Petter Selasky } else { 1698f54ab96dSHans Petter Selasky err = USB_ERR_INVAL; 1699f54ab96dSHans Petter Selasky } 1700f54ab96dSHans Petter Selasky 1701f54ab96dSHans Petter Selasky if (err || (scratch_ptr[0] < 4)) { 1702f54ab96dSHans Petter Selasky udev->flags.no_strings = 1; 1703f54ab96dSHans Petter Selasky } else { 1704f54ab96dSHans Petter Selasky uint16_t langid; 1705f54ab96dSHans Petter Selasky uint16_t pref; 1706f54ab96dSHans Petter Selasky uint16_t mask; 1707f54ab96dSHans Petter Selasky uint8_t x; 1708f54ab96dSHans Petter Selasky 1709f54ab96dSHans Petter Selasky /* load preferred value and mask */ 1710f54ab96dSHans Petter Selasky pref = usb_lang_id; 1711f54ab96dSHans Petter Selasky mask = usb_lang_mask; 1712f54ab96dSHans Petter Selasky 1713f54ab96dSHans Petter Selasky /* align length correctly */ 1714f54ab96dSHans Petter Selasky scratch_ptr[0] &= ~1U; 1715f54ab96dSHans Petter Selasky 1716f54ab96dSHans Petter Selasky /* fix compiler warning */ 1717f54ab96dSHans Petter Selasky langid = 0; 1718f54ab96dSHans Petter Selasky 1719f54ab96dSHans Petter Selasky /* search for preferred language */ 1720f54ab96dSHans Petter Selasky for (x = 2; x < scratch_ptr[0]; x += 2) { 1721f54ab96dSHans Petter Selasky langid = UGETW(scratch_ptr + x); 1722f54ab96dSHans Petter Selasky if ((langid & mask) == pref) 1723f54ab96dSHans Petter Selasky break; 1724f54ab96dSHans Petter Selasky } 1725f54ab96dSHans Petter Selasky if (x >= scratch_ptr[0]) { 1726f54ab96dSHans Petter Selasky /* pick the first language as the default */ 1727f54ab96dSHans Petter Selasky DPRINTFN(1, "Using first language\n"); 1728f54ab96dSHans Petter Selasky langid = UGETW(scratch_ptr + 2); 1729f54ab96dSHans Petter Selasky } 1730f54ab96dSHans Petter Selasky 1731f54ab96dSHans Petter Selasky DPRINTFN(1, "Language selected: 0x%04x\n", langid); 1732f54ab96dSHans Petter Selasky udev->langid = langid; 1733f54ab96dSHans Petter Selasky } 1734f54ab96dSHans Petter Selasky 1735f54ab96dSHans Petter Selasky if (do_unlock) 1736f54ab96dSHans Petter Selasky usbd_ctrl_unlock(udev); 1737f54ab96dSHans Petter Selasky } 1738f54ab96dSHans Petter Selasky 1739f54ab96dSHans Petter Selasky /*------------------------------------------------------------------------* 1740a593f6b8SAndrew Thompson * usb_alloc_device 174102ac6454SAndrew Thompson * 174202ac6454SAndrew Thompson * This function allocates a new USB device. This function is called 174302ac6454SAndrew Thompson * when a new device has been put in the powered state, but not yet in 174402ac6454SAndrew Thompson * the addressed state. Get initial descriptor, set the address, get 174502ac6454SAndrew Thompson * full descriptor and get strings. 174602ac6454SAndrew Thompson * 174702ac6454SAndrew Thompson * Return values: 174802ac6454SAndrew Thompson * 0: Failure 174902ac6454SAndrew Thompson * Else: Success 175002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 1751760bc48eSAndrew Thompson struct usb_device * 1752a593f6b8SAndrew Thompson usb_alloc_device(device_t parent_dev, struct usb_bus *bus, 1753760bc48eSAndrew Thompson struct usb_device *parent_hub, uint8_t depth, uint8_t port_index, 17548d2dd5ddSAndrew Thompson uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode) 175502ac6454SAndrew Thompson { 1756760bc48eSAndrew Thompson struct usb_attach_arg uaa; 1757760bc48eSAndrew Thompson struct usb_device *udev; 1758760bc48eSAndrew Thompson struct usb_device *adev; 1759760bc48eSAndrew Thompson struct usb_device *hub; 1760e0a69b51SAndrew Thompson usb_error_t err; 176102ac6454SAndrew Thompson uint8_t device_index; 17622a4c6157SAndrew Thompson uint8_t config_index; 17632a4c6157SAndrew Thompson uint8_t config_quirk; 17642a4c6157SAndrew Thompson uint8_t set_config_failed; 176502ac6454SAndrew Thompson 176602ac6454SAndrew Thompson DPRINTF("parent_dev=%p, bus=%p, parent_hub=%p, depth=%u, " 1767f29a0724SAndrew Thompson "port_index=%u, port_no=%u, speed=%u, usb_mode=%u\n", 176802ac6454SAndrew Thompson parent_dev, bus, parent_hub, depth, port_index, port_no, 1769f29a0724SAndrew Thompson speed, mode); 177002ac6454SAndrew Thompson 177102ac6454SAndrew Thompson /* 177202ac6454SAndrew Thompson * Find an unused device index. In USB Host mode this is the 177302ac6454SAndrew Thompson * same as the device address. 177402ac6454SAndrew Thompson * 177502ac6454SAndrew Thompson * Device index zero is not used and device index 1 should 177602ac6454SAndrew Thompson * always be the root hub. 177702ac6454SAndrew Thompson */ 177802ac6454SAndrew Thompson for (device_index = USB_ROOT_HUB_ADDR; 177902ac6454SAndrew Thompson (device_index != bus->devices_max) && 178002ac6454SAndrew Thompson (bus->devices[device_index] != NULL); 178102ac6454SAndrew Thompson device_index++) /* nop */; 178202ac6454SAndrew Thompson 178302ac6454SAndrew Thompson if (device_index == bus->devices_max) { 178402ac6454SAndrew Thompson device_printf(bus->bdev, 1785767cb2e2SAndrew Thompson "No free USB device index for new device\n"); 178602ac6454SAndrew Thompson return (NULL); 178702ac6454SAndrew Thompson } 178802ac6454SAndrew Thompson 178902ac6454SAndrew Thompson if (depth > 0x10) { 179002ac6454SAndrew Thompson device_printf(bus->bdev, 1791767cb2e2SAndrew Thompson "Invalid device depth\n"); 179202ac6454SAndrew Thompson return (NULL); 179302ac6454SAndrew Thompson } 179402ac6454SAndrew Thompson udev = malloc(sizeof(*udev), M_USB, M_WAITOK | M_ZERO); 179594140f47SMark Johnston #if (USB_HAVE_MALLOC_WAITOK == 0) 179602ac6454SAndrew Thompson if (udev == NULL) { 179702ac6454SAndrew Thompson return (NULL); 179802ac6454SAndrew Thompson } 179994140f47SMark Johnston #endif 180002ac6454SAndrew Thompson /* initialise our SX-lock */ 180191cd9240SAndrew Thompson sx_init_flags(&udev->enum_sx, "USB config SX lock", SX_DUPOK); 1802a6b60150SHans Petter Selasky sx_init_flags(&udev->sr_sx, "USB suspend and resume SX lock", SX_NOWITNESS); 180364cb5e2aSHans Petter Selasky sx_init_flags(&udev->ctrl_sx, "USB control transfer SX lock", SX_DUPOK); 180402ac6454SAndrew Thompson 180591cd9240SAndrew Thompson cv_init(&udev->ctrlreq_cv, "WCTRL"); 180691cd9240SAndrew Thompson cv_init(&udev->ref_cv, "UGONE"); 180702ac6454SAndrew Thompson 180802ac6454SAndrew Thompson /* initialise our mutex */ 180991cd9240SAndrew Thompson mtx_init(&udev->device_mtx, "USB device mutex", NULL, MTX_DEF); 181002ac6454SAndrew Thompson 181102ac6454SAndrew Thompson /* initialise generic clear stall */ 1812a593f6b8SAndrew Thompson udev->cs_msg[0].hdr.pm_callback = &usbd_clear_stall_proc; 181302ac6454SAndrew Thompson udev->cs_msg[0].udev = udev; 1814a593f6b8SAndrew Thompson udev->cs_msg[1].hdr.pm_callback = &usbd_clear_stall_proc; 181502ac6454SAndrew Thompson udev->cs_msg[1].udev = udev; 181602ac6454SAndrew Thompson 181702ac6454SAndrew Thompson /* initialise some USB device fields */ 181802ac6454SAndrew Thompson udev->parent_hub = parent_hub; 181902ac6454SAndrew Thompson udev->parent_dev = parent_dev; 182002ac6454SAndrew Thompson udev->port_index = port_index; 182102ac6454SAndrew Thompson udev->port_no = port_no; 182202ac6454SAndrew Thompson udev->depth = depth; 182302ac6454SAndrew Thompson udev->bus = bus; 182402ac6454SAndrew Thompson udev->address = USB_START_ADDR; /* default value */ 1825e0a69b51SAndrew Thompson udev->plugtime = (usb_ticks_t)ticks; 182602ac6454SAndrew Thompson /* 182702ac6454SAndrew Thompson * We need to force the power mode to "on" because there are plenty 182802ac6454SAndrew Thompson * of USB devices out there that do not work very well with 182902ac6454SAndrew Thompson * automatic suspend and resume! 183002ac6454SAndrew Thompson */ 1831d2d71ce7SAndrew Thompson udev->power_mode = usbd_filter_power_mode(udev, USB_POWER_MODE_ON); 183202ac6454SAndrew Thompson udev->pwr_save.last_xfer_time = ticks; 183302ac6454SAndrew Thompson /* we are not ready yet */ 183402ac6454SAndrew Thompson udev->refcount = 1; 183502ac6454SAndrew Thompson 183602ac6454SAndrew Thompson /* set up default endpoint descriptor */ 18375b3bb704SAndrew Thompson udev->ctrl_ep_desc.bLength = sizeof(udev->ctrl_ep_desc); 18385b3bb704SAndrew Thompson udev->ctrl_ep_desc.bDescriptorType = UDESC_ENDPOINT; 18395b3bb704SAndrew Thompson udev->ctrl_ep_desc.bEndpointAddress = USB_CONTROL_ENDPOINT; 18405b3bb704SAndrew Thompson udev->ctrl_ep_desc.bmAttributes = UE_CONTROL; 18415b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[0] = USB_MAX_IPACKET; 18425b3bb704SAndrew Thompson udev->ctrl_ep_desc.wMaxPacketSize[1] = 0; 18435b3bb704SAndrew Thompson udev->ctrl_ep_desc.bInterval = 0; 1844963169b4SHans Petter Selasky 1845963169b4SHans Petter Selasky /* set up default endpoint companion descriptor */ 1846963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bLength = sizeof(udev->ctrl_ep_comp_desc); 1847963169b4SHans Petter Selasky udev->ctrl_ep_comp_desc.bDescriptorType = UDESC_ENDPOINT_SS_COMP; 1848963169b4SHans Petter Selasky 184902ac6454SAndrew Thompson udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 185002ac6454SAndrew Thompson 185102ac6454SAndrew Thompson udev->speed = speed; 1852f29a0724SAndrew Thompson udev->flags.usb_mode = mode; 185302ac6454SAndrew Thompson 185402ac6454SAndrew Thompson /* search for our High Speed USB HUB, if any */ 185502ac6454SAndrew Thompson 185602ac6454SAndrew Thompson adev = udev; 185702ac6454SAndrew Thompson hub = udev->parent_hub; 185802ac6454SAndrew Thompson 185902ac6454SAndrew Thompson while (hub) { 186002ac6454SAndrew Thompson if (hub->speed == USB_SPEED_HIGH) { 186102ac6454SAndrew Thompson udev->hs_hub_addr = hub->address; 1862495f25ceSAndrew Thompson udev->parent_hs_hub = hub; 186302ac6454SAndrew Thompson udev->hs_port_no = adev->port_no; 186402ac6454SAndrew Thompson break; 186502ac6454SAndrew Thompson } 186602ac6454SAndrew Thompson adev = hub; 186702ac6454SAndrew Thompson hub = hub->parent_hub; 186802ac6454SAndrew Thompson } 186902ac6454SAndrew Thompson 1870ae60fdfbSAndrew Thompson /* init the default endpoint */ 1871a593f6b8SAndrew Thompson usb_init_endpoint(udev, 0, 18725b3bb704SAndrew Thompson &udev->ctrl_ep_desc, 1873963169b4SHans Petter Selasky &udev->ctrl_ep_comp_desc, 18745b3bb704SAndrew Thompson &udev->ctrl_ep); 187502ac6454SAndrew Thompson 187602ac6454SAndrew Thompson /* set device index */ 187702ac6454SAndrew Thompson udev->device_index = device_index; 187802ac6454SAndrew Thompson 18798755859aSAndrew Thompson #if USB_HAVE_UGEN 18800f6e8f93SAndrew Thompson /* Create ugen name */ 1881ee3e3ff5SAndrew Thompson snprintf(udev->ugen_name, sizeof(udev->ugen_name), 1882ee3e3ff5SAndrew Thompson USB_GENERIC_NAME "%u.%u", device_get_unit(bus->bdev), 1883ee3e3ff5SAndrew Thompson device_index); 18848755859aSAndrew Thompson LIST_INIT(&udev->pd_list); 18858755859aSAndrew Thompson 18860f6e8f93SAndrew Thompson /* Create the control endpoint device */ 18872ffd5fdcSHans Petter Selasky udev->ctrl_dev = usb_make_dev(udev, NULL, 0, 0, 18882ffd5fdcSHans Petter Selasky FREAD|FWRITE, UID_ROOT, GID_OPERATOR, 0600); 18890f6e8f93SAndrew Thompson 18900f6e8f93SAndrew Thompson /* Create a link from /dev/ugenX.X to the default endpoint */ 18912ffd5fdcSHans Petter Selasky if (udev->ctrl_dev != NULL) 18922ffd5fdcSHans Petter Selasky make_dev_alias(udev->ctrl_dev->cdev, "%s", udev->ugen_name); 18930f6e8f93SAndrew Thompson #endif 1894963169b4SHans Petter Selasky /* Initialise device */ 1895963169b4SHans Petter Selasky if (bus->methods->device_init != NULL) { 1896963169b4SHans Petter Selasky err = (bus->methods->device_init) (udev); 1897963169b4SHans Petter Selasky if (err != 0) { 1898963169b4SHans Petter Selasky DPRINTFN(0, "device init %d failed " 1899963169b4SHans Petter Selasky "(%s, ignored)\n", device_index, 1900963169b4SHans Petter Selasky usbd_errstr(err)); 1901963169b4SHans Petter Selasky goto done; 1902963169b4SHans Petter Selasky } 1903963169b4SHans Petter Selasky } 1904963169b4SHans Petter Selasky /* set powered device state after device init is complete */ 1905963169b4SHans Petter Selasky usb_set_device_state(udev, USB_STATE_POWERED); 1906963169b4SHans Petter Selasky 1907f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_HOST) { 1908a593f6b8SAndrew Thompson err = usbd_req_set_address(udev, NULL, device_index); 190902ac6454SAndrew Thompson 1910963169b4SHans Petter Selasky /* 1911963169b4SHans Petter Selasky * This is the new USB device address from now on, if 1912963169b4SHans Petter Selasky * the set address request didn't set it already. 1913963169b4SHans Petter Selasky */ 1914963169b4SHans Petter Selasky if (udev->address == USB_START_ADDR) 191502ac6454SAndrew Thompson udev->address = device_index; 191602ac6454SAndrew Thompson 191702ac6454SAndrew Thompson /* 191802ac6454SAndrew Thompson * We ignore any set-address errors, hence there are 191902ac6454SAndrew Thompson * buggy USB devices out there that actually receive 192002ac6454SAndrew Thompson * the SETUP PID, but manage to set the address before 192102ac6454SAndrew Thompson * the STATUS stage is ACK'ed. If the device responds 192202ac6454SAndrew Thompson * to the subsequent get-descriptor at the new 192302ac6454SAndrew Thompson * address, then we know that the set-address command 192402ac6454SAndrew Thompson * was successful. 192502ac6454SAndrew Thompson */ 192602ac6454SAndrew Thompson if (err) { 192702ac6454SAndrew Thompson DPRINTFN(0, "set address %d failed " 192803797f33SAndrew Thompson "(%s, ignored)\n", udev->address, 1929a593f6b8SAndrew Thompson usbd_errstr(err)); 193002ac6454SAndrew Thompson } 193102ac6454SAndrew Thompson } else { 193202ac6454SAndrew Thompson /* We are not self powered */ 193302ac6454SAndrew Thompson udev->flags.self_powered = 0; 193402ac6454SAndrew Thompson 193502ac6454SAndrew Thompson /* Set unconfigured state */ 193602ac6454SAndrew Thompson udev->curr_config_no = USB_UNCONFIG_NO; 193702ac6454SAndrew Thompson udev->curr_config_index = USB_UNCONFIG_INDEX; 193802ac6454SAndrew Thompson 193902ac6454SAndrew Thompson /* Setup USB descriptors */ 1940a593f6b8SAndrew Thompson err = (usb_temp_setup_by_index_p) (udev, usb_template); 194102ac6454SAndrew Thompson if (err) { 19426be75992SEdward Tomasz Napierala DPRINTFN(0, "setting up USB template failed - " 19436be75992SEdward Tomasz Napierala "usb_template(4) not loaded?\n"); 194402ac6454SAndrew Thompson goto done; 194502ac6454SAndrew Thompson } 194602ac6454SAndrew Thompson } 1947a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_ADDRESSED); 194802ac6454SAndrew Thompson 1949963169b4SHans Petter Selasky /* setup the device descriptor and the initial "wMaxPacketSize" */ 1950963169b4SHans Petter Selasky err = usbd_setup_device_desc(udev, NULL); 1951963169b4SHans Petter Selasky 1952963169b4SHans Petter Selasky if (err != 0) { 195323de050bSHans Petter Selasky /* try to enumerate two more times */ 1954a593f6b8SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 195523de050bSHans Petter Selasky if (err != 0) { 195623de050bSHans Petter Selasky err = usbd_req_re_enumerate(udev, NULL); 195723de050bSHans Petter Selasky if (err != 0) { 195802ac6454SAndrew Thompson goto done; 195902ac6454SAndrew Thompson } 196023de050bSHans Petter Selasky } 196123de050bSHans Petter Selasky } 196202ac6454SAndrew Thompson 196302ac6454SAndrew Thompson /* 196402ac6454SAndrew Thompson * Setup temporary USB attach args so that we can figure out some 196502ac6454SAndrew Thompson * basic quirks for this device. 196602ac6454SAndrew Thompson */ 1967a593f6b8SAndrew Thompson usb_init_attach_arg(udev, &uaa); 196802ac6454SAndrew Thompson 1969a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_BUS_POWERED)) { 197002ac6454SAndrew Thompson udev->flags.uq_bus_powered = 1; 197102ac6454SAndrew Thompson } 1972a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_NO_STRINGS)) { 197302ac6454SAndrew Thompson udev->flags.no_strings = 1; 197402ac6454SAndrew Thompson } 19756950c75fSHans Petter Selasky 1976f54ab96dSHans Petter Selasky usb_get_langid(udev); 19776950c75fSHans Petter Selasky 197802ac6454SAndrew Thompson /* assume 100mA bus powered for now. Changed when configured. */ 197902ac6454SAndrew Thompson udev->power = USB_MIN_POWER; 1980c86aead6SAndrew Thompson /* fetch the vendor and product strings from the device */ 1981f54ab96dSHans Petter Selasky usb_set_device_strings(udev); 198202ac6454SAndrew Thompson 19832a4c6157SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 19842a4c6157SAndrew Thompson /* USB device mode setup is complete */ 19852a4c6157SAndrew Thompson err = 0; 19862a4c6157SAndrew Thompson goto config_done; 19872a4c6157SAndrew Thompson } 198802ac6454SAndrew Thompson 198902ac6454SAndrew Thompson /* 199002ac6454SAndrew Thompson * Most USB devices should attach to config index 0 by 199102ac6454SAndrew Thompson * default 199202ac6454SAndrew Thompson */ 1993a593f6b8SAndrew Thompson if (usb_test_quirk(&uaa, UQ_CFG_INDEX_0)) { 199402ac6454SAndrew Thompson config_index = 0; 199502ac6454SAndrew Thompson config_quirk = 1; 1996a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_1)) { 199702ac6454SAndrew Thompson config_index = 1; 199802ac6454SAndrew Thompson config_quirk = 1; 1999a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_2)) { 200002ac6454SAndrew Thompson config_index = 2; 200102ac6454SAndrew Thompson config_quirk = 1; 2002a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_3)) { 200302ac6454SAndrew Thompson config_index = 3; 200402ac6454SAndrew Thompson config_quirk = 1; 2005a593f6b8SAndrew Thompson } else if (usb_test_quirk(&uaa, UQ_CFG_INDEX_4)) { 200602ac6454SAndrew Thompson config_index = 4; 200702ac6454SAndrew Thompson config_quirk = 1; 200802ac6454SAndrew Thompson } else { 200902ac6454SAndrew Thompson config_index = 0; 201002ac6454SAndrew Thompson config_quirk = 0; 201102ac6454SAndrew Thompson } 201202ac6454SAndrew Thompson 20132a4c6157SAndrew Thompson set_config_failed = 0; 201402ac6454SAndrew Thompson repeat_set_config: 201502ac6454SAndrew Thompson 201602ac6454SAndrew Thompson DPRINTF("setting config %u\n", config_index); 201702ac6454SAndrew Thompson 201802ac6454SAndrew Thompson /* get the USB device configured */ 2019a593f6b8SAndrew Thompson err = usbd_set_config_index(udev, config_index); 202002ac6454SAndrew Thompson if (err) { 202102ac6454SAndrew Thompson if (udev->ddesc.bNumConfigurations != 0) { 202202ac6454SAndrew Thompson if (!set_config_failed) { 202302ac6454SAndrew Thompson set_config_failed = 1; 202402ac6454SAndrew Thompson /* XXX try to re-enumerate the device */ 20252a4c6157SAndrew Thompson err = usbd_req_re_enumerate(udev, NULL); 202602ac6454SAndrew Thompson if (err == 0) 202702ac6454SAndrew Thompson goto repeat_set_config; 202802ac6454SAndrew Thompson } 20292a4c6157SAndrew Thompson DPRINTFN(0, "Failure selecting configuration index %u:" 20302a4c6157SAndrew Thompson "%s, port %u, addr %u (ignored)\n", 2031a593f6b8SAndrew Thompson config_index, usbd_errstr(err), udev->port_no, 203202ac6454SAndrew Thompson udev->address); 203302ac6454SAndrew Thompson } 203402ac6454SAndrew Thompson /* 20352a4c6157SAndrew Thompson * Some USB devices do not have any configurations. Ignore any 20362a4c6157SAndrew Thompson * set config failures! 203702ac6454SAndrew Thompson */ 203802ac6454SAndrew Thompson err = 0; 20392a4c6157SAndrew Thompson goto config_done; 20402a4c6157SAndrew Thompson } 20412a4c6157SAndrew Thompson if (!config_quirk && config_index + 1 < udev->ddesc.bNumConfigurations) { 204202ac6454SAndrew Thompson if ((udev->cdesc->bNumInterface < 2) && 20432a4c6157SAndrew Thompson usbd_get_no_descriptors(udev->cdesc, UDESC_ENDPOINT) == 0) { 20442a4c6157SAndrew Thompson DPRINTFN(0, "Found no endpoints, trying next config\n"); 204502ac6454SAndrew Thompson config_index++; 204602ac6454SAndrew Thompson goto repeat_set_config; 204702ac6454SAndrew Thompson } 204815d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 20493ee94386SKornel Duleba if (config_index == 0 && 20503ee94386SKornel Duleba usb_test_quirk(&uaa, UQ_MSC_NO_INQUIRY) == 0) { 205102ac6454SAndrew Thompson /* 205202ac6454SAndrew Thompson * Try to figure out if we have an 205302ac6454SAndrew Thompson * auto-install disk there: 205402ac6454SAndrew Thompson */ 2055d84a79e7SAndrew Thompson if (usb_iface_is_cdrom(udev, 0)) { 205602ac6454SAndrew Thompson DPRINTFN(0, "Found possible auto-install " 205702ac6454SAndrew Thompson "disk (trying next config)\n"); 205802ac6454SAndrew Thompson config_index++; 205902ac6454SAndrew Thompson goto repeat_set_config; 206002ac6454SAndrew Thompson } 206102ac6454SAndrew Thompson } 206215d4edd3SHans Petter Selasky #endif 20632a4c6157SAndrew Thompson } 206415d4edd3SHans Petter Selasky #if USB_HAVE_MSCTEST 2065d46dc4adSHans Petter Selasky if (set_config_failed == 0 && config_index == 0 && 20667520b888SHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_START_STOP) == 0 && 20677520b888SHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_PREVENT_ALLOW) == 0 && 20689157ad4bSHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_SYNC_CACHE) == 0 && 20697520b888SHans Petter Selasky usb_test_quirk(&uaa, UQ_MSC_NO_TEST_UNIT_READY) == 0 && 20703ee94386SKornel Duleba usb_test_quirk(&uaa, UQ_MSC_NO_GETMAXLUN) == 0 && 20713ee94386SKornel Duleba usb_test_quirk(&uaa, UQ_MSC_NO_INQUIRY) == 0) { 2072d46dc4adSHans Petter Selasky /* 2073d46dc4adSHans Petter Selasky * Try to figure out if there are any MSC quirks we 2074d46dc4adSHans Petter Selasky * should apply automatically: 2075d46dc4adSHans Petter Selasky */ 20767520b888SHans Petter Selasky err = usb_msc_auto_quirk(udev, 0, &uaa); 2077d46dc4adSHans Petter Selasky 2078d46dc4adSHans Petter Selasky if (err != 0) { 2079d46dc4adSHans Petter Selasky set_config_failed = 1; 2080d46dc4adSHans Petter Selasky goto repeat_set_config; 2081d46dc4adSHans Petter Selasky } 2082d46dc4adSHans Petter Selasky } 208315d4edd3SHans Petter Selasky #endif 208402ac6454SAndrew Thompson 20852a4c6157SAndrew Thompson config_done: 208602ac6454SAndrew Thompson DPRINTF("new dev (addr %d), udev=%p, parent_hub=%p\n", 208702ac6454SAndrew Thompson udev->address, udev, udev->parent_hub); 208802ac6454SAndrew Thompson 208902ac6454SAndrew Thompson /* register our device - we are ready */ 2090a593f6b8SAndrew Thompson usb_bus_port_set_device(bus, parent_hub ? 209102ac6454SAndrew Thompson parent_hub->hub->ports + port_index : NULL, udev, device_index); 209202ac6454SAndrew Thompson 20930f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 20948755859aSAndrew Thompson /* Symlink the ugen device name */ 2095a593f6b8SAndrew Thompson udev->ugen_symlink = usb_alloc_symlink(udev->ugen_name); 20968755859aSAndrew Thompson 20978755859aSAndrew Thompson /* Announce device */ 209888f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s\n", udev->ugen_name, 209988f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 210002ac6454SAndrew Thompson device_get_nameunit(udev->bus->bdev)); 21018bb77249SHans Petter Selasky #endif 21027efaaa9aSAndrew Thompson 21038bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 21043acd904dSAndrew Thompson usb_notify_addq("ATTACH", udev); 21058755859aSAndrew Thompson #endif 210602ac6454SAndrew Thompson done: 210702ac6454SAndrew Thompson if (err) { 2108d88688c7SAndrew Thompson /* 2109d88688c7SAndrew Thompson * Free USB device and all subdevices, if any. 2110d88688c7SAndrew Thompson */ 2111d88688c7SAndrew Thompson usb_free_device(udev, 0); 211202ac6454SAndrew Thompson udev = NULL; 211302ac6454SAndrew Thompson } 211402ac6454SAndrew Thompson return (udev); 211502ac6454SAndrew Thompson } 211602ac6454SAndrew Thompson 21170f6e8f93SAndrew Thompson #if USB_HAVE_UGEN 21182ffd5fdcSHans Petter Selasky struct usb_fs_privdata * 21192ffd5fdcSHans Petter Selasky usb_make_dev(struct usb_device *udev, const char *devname, int ep, 21202ffd5fdcSHans Petter Selasky int fi, int rwmode, uid_t uid, gid_t gid, int mode) 2121ee3e3ff5SAndrew Thompson { 2122760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2123ac3490fdSHans Petter Selasky struct make_dev_args args; 21242ffd5fdcSHans Petter Selasky char buffer[32]; 2125ee3e3ff5SAndrew Thompson 2126ee3e3ff5SAndrew Thompson /* Store information to locate ourselves again later */ 2127760bc48eSAndrew Thompson pd = malloc(sizeof(struct usb_fs_privdata), M_USBDEV, 2128ee3e3ff5SAndrew Thompson M_WAITOK | M_ZERO); 2129ee3e3ff5SAndrew Thompson pd->bus_index = device_get_unit(udev->bus->bdev); 2130ee3e3ff5SAndrew Thompson pd->dev_index = udev->device_index; 2131ee3e3ff5SAndrew Thompson pd->ep_addr = ep; 21322ffd5fdcSHans Petter Selasky pd->fifo_index = fi; 21332ffd5fdcSHans Petter Selasky pd->mode = rwmode; 2134ee3e3ff5SAndrew Thompson 2135ee3e3ff5SAndrew Thompson /* Now, create the device itself */ 21362ffd5fdcSHans Petter Selasky if (devname == NULL) { 21372ffd5fdcSHans Petter Selasky devname = buffer; 21382ffd5fdcSHans Petter Selasky snprintf(buffer, sizeof(buffer), USB_DEVICE_DIR "/%u.%u.%u", 2139f35aaff0SAndrew Thompson pd->bus_index, pd->dev_index, pd->ep_addr); 21402ffd5fdcSHans Petter Selasky } 21412ffd5fdcSHans Petter Selasky 2142ac3490fdSHans Petter Selasky /* Setup arguments for make_dev_s() */ 2143ac3490fdSHans Petter Selasky make_dev_args_init(&args); 2144ac3490fdSHans Petter Selasky args.mda_devsw = &usb_devsw; 2145ac3490fdSHans Petter Selasky args.mda_uid = uid; 2146ac3490fdSHans Petter Selasky args.mda_gid = gid; 2147ac3490fdSHans Petter Selasky args.mda_mode = mode; 2148ac3490fdSHans Petter Selasky args.mda_si_drv1 = pd; 21492ffd5fdcSHans Petter Selasky 2150ac3490fdSHans Petter Selasky if (make_dev_s(&args, &pd->cdev, "%s", devname) != 0) { 21512ffd5fdcSHans Petter Selasky DPRINTFN(0, "Failed to create device %s\n", devname); 21522ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 21532ffd5fdcSHans Petter Selasky return (NULL); 21542ffd5fdcSHans Petter Selasky } 21552ffd5fdcSHans Petter Selasky return (pd); 21562ffd5fdcSHans Petter Selasky } 21572ffd5fdcSHans Petter Selasky 21582ffd5fdcSHans Petter Selasky void 2159b78e84d1SHans Petter Selasky usb_destroy_dev_sync(struct usb_fs_privdata *pd) 21602ffd5fdcSHans Petter Selasky { 2161b78e84d1SHans Petter Selasky DPRINTFN(1, "Destroying device at ugen%d.%d\n", 2162b78e84d1SHans Petter Selasky pd->bus_index, pd->dev_index); 21632ffd5fdcSHans Petter Selasky 2164b78e84d1SHans Petter Selasky /* 2165b78e84d1SHans Petter Selasky * Destroy character device synchronously. After this 2166b78e84d1SHans Petter Selasky * all system calls are returned. Can block. 2167b78e84d1SHans Petter Selasky */ 21682ffd5fdcSHans Petter Selasky destroy_dev(pd->cdev); 21692ffd5fdcSHans Petter Selasky 21702ffd5fdcSHans Petter Selasky free(pd, M_USBDEV); 2171ee3e3ff5SAndrew Thompson } 2172ee3e3ff5SAndrew Thompson 2173b78e84d1SHans Petter Selasky void 2174b78e84d1SHans Petter Selasky usb_destroy_dev(struct usb_fs_privdata *pd) 2175b78e84d1SHans Petter Selasky { 2176b78e84d1SHans Petter Selasky struct usb_bus *bus; 2177b78e84d1SHans Petter Selasky 2178b78e84d1SHans Petter Selasky if (pd == NULL) 2179b78e84d1SHans Petter Selasky return; 2180b78e84d1SHans Petter Selasky 2181b78e84d1SHans Petter Selasky mtx_lock(&usb_ref_lock); 2182b78e84d1SHans Petter Selasky bus = devclass_get_softc(usb_devclass_ptr, pd->bus_index); 2183b78e84d1SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2184b78e84d1SHans Petter Selasky 2185b78e84d1SHans Petter Selasky if (bus == NULL) { 2186b78e84d1SHans Petter Selasky usb_destroy_dev_sync(pd); 2187b78e84d1SHans Petter Selasky return; 2188b78e84d1SHans Petter Selasky } 2189b78e84d1SHans Petter Selasky 219007dbde67SHans Petter Selasky /* make sure we can re-use the device name */ 219107dbde67SHans Petter Selasky delist_dev(pd->cdev); 219207dbde67SHans Petter Selasky 2193b78e84d1SHans Petter Selasky USB_BUS_LOCK(bus); 2194b78e84d1SHans Petter Selasky LIST_INSERT_HEAD(&bus->pd_cleanup_list, pd, pd_next); 2195b78e84d1SHans Petter Selasky /* get cleanup going */ 2196b78e84d1SHans Petter Selasky usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus), 2197b78e84d1SHans Petter Selasky &bus->cleanup_msg[0], &bus->cleanup_msg[1]); 2198b78e84d1SHans Petter Selasky USB_BUS_UNLOCK(bus); 2199b78e84d1SHans Petter Selasky } 2200b78e84d1SHans Petter Selasky 2201ee3e3ff5SAndrew Thompson static void 2202a593f6b8SAndrew Thompson usb_cdev_create(struct usb_device *udev) 2203ee3e3ff5SAndrew Thompson { 2204760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2205760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ed; 2206760bc48eSAndrew Thompson struct usb_descriptor *desc; 2207760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2208f5f145baSAndrew Thompson int inmode, outmode, inmask, outmask, mode; 2209f5f145baSAndrew Thompson uint8_t ep; 2210ee3e3ff5SAndrew Thompson 2211ee3e3ff5SAndrew Thompson KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("stale cdev entries")); 2212ee3e3ff5SAndrew Thompson 2213ee3e3ff5SAndrew Thompson DPRINTFN(2, "Creating device nodes\n"); 2214ee3e3ff5SAndrew Thompson 2215a593f6b8SAndrew Thompson if (usbd_get_mode(udev) == USB_MODE_DEVICE) { 2216ee3e3ff5SAndrew Thompson inmode = FWRITE; 2217ee3e3ff5SAndrew Thompson outmode = FREAD; 2218ee3e3ff5SAndrew Thompson } else { /* USB_MODE_HOST */ 2219ee3e3ff5SAndrew Thompson inmode = FREAD; 2220ee3e3ff5SAndrew Thompson outmode = FWRITE; 2221ee3e3ff5SAndrew Thompson } 2222ee3e3ff5SAndrew Thompson 2223f5f145baSAndrew Thompson inmask = 0; 2224f5f145baSAndrew Thompson outmask = 0; 2225f5f145baSAndrew Thompson desc = NULL; 2226f5f145baSAndrew Thompson 2227f5f145baSAndrew Thompson /* 2228f5f145baSAndrew Thompson * Collect all used endpoint numbers instead of just 2229f5f145baSAndrew Thompson * generating 16 static endpoints. 2230f5f145baSAndrew Thompson */ 2231a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 2232a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 2233f5f145baSAndrew Thompson /* filter out all endpoint descriptors */ 2234f5f145baSAndrew Thompson if ((desc->bDescriptorType == UDESC_ENDPOINT) && 2235f5f145baSAndrew Thompson (desc->bLength >= sizeof(*ed))) { 2236760bc48eSAndrew Thompson ed = (struct usb_endpoint_descriptor *)desc; 2237f5f145baSAndrew Thompson 2238f5f145baSAndrew Thompson /* update masks */ 2239f5f145baSAndrew Thompson ep = ed->bEndpointAddress; 2240f5f145baSAndrew Thompson if (UE_GET_DIR(ep) == UE_DIR_OUT) 2241f5f145baSAndrew Thompson outmask |= 1 << UE_GET_ADDR(ep); 2242f5f145baSAndrew Thompson else 2243f5f145baSAndrew Thompson inmask |= 1 << UE_GET_ADDR(ep); 2244f5f145baSAndrew Thompson } 2245f5f145baSAndrew Thompson } 2246ee3e3ff5SAndrew Thompson 2247ee3e3ff5SAndrew Thompson /* Create all available endpoints except EP0 */ 2248ee3e3ff5SAndrew Thompson for (ep = 1; ep < 16; ep++) { 22492ffd5fdcSHans Petter Selasky mode = (inmask & (1 << ep)) ? inmode : 0; 22502ffd5fdcSHans Petter Selasky mode |= (outmask & (1 << ep)) ? outmode : 0; 2251ee3e3ff5SAndrew Thompson if (mode == 0) 2252ee3e3ff5SAndrew Thompson continue; /* no IN or OUT endpoint */ 2253ee3e3ff5SAndrew Thompson 22542ffd5fdcSHans Petter Selasky pd = usb_make_dev(udev, NULL, ep, 0, 22552ffd5fdcSHans Petter Selasky mode, UID_ROOT, GID_OPERATOR, 0600); 22562ffd5fdcSHans Petter Selasky 22572ffd5fdcSHans Petter Selasky if (pd != NULL) 2258ee3e3ff5SAndrew Thompson LIST_INSERT_HEAD(&udev->pd_list, pd, pd_next); 2259ee3e3ff5SAndrew Thompson } 2260ee3e3ff5SAndrew Thompson } 2261ee3e3ff5SAndrew Thompson 2262ee3e3ff5SAndrew Thompson static void 2263a593f6b8SAndrew Thompson usb_cdev_free(struct usb_device *udev) 2264ee3e3ff5SAndrew Thompson { 2265760bc48eSAndrew Thompson struct usb_fs_privdata* pd; 2266ee3e3ff5SAndrew Thompson 2267ee3e3ff5SAndrew Thompson DPRINTFN(2, "Freeing device nodes\n"); 2268ee3e3ff5SAndrew Thompson 2269ee3e3ff5SAndrew Thompson while ((pd = LIST_FIRST(&udev->pd_list)) != NULL) { 2270ee3e3ff5SAndrew Thompson KASSERT(pd->cdev->si_drv1 == pd, ("privdata corrupt")); 2271ee3e3ff5SAndrew Thompson 2272ee3e3ff5SAndrew Thompson LIST_REMOVE(pd, pd_next); 2273ee3e3ff5SAndrew Thompson 22742ffd5fdcSHans Petter Selasky usb_destroy_dev(pd); 22752ffd5fdcSHans Petter Selasky } 2276ee3e3ff5SAndrew Thompson } 22770f6e8f93SAndrew Thompson #endif 2278ee3e3ff5SAndrew Thompson 227902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2280a593f6b8SAndrew Thompson * usb_free_device 228102ac6454SAndrew Thompson * 2282d88688c7SAndrew Thompson * This function is NULL safe and will free an USB device and its 2283d88688c7SAndrew Thompson * children devices, if any. 2284bdd41206SAndrew Thompson * 2285d88688c7SAndrew Thompson * Flag values: Reserved, set to zero. 228602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 228702ac6454SAndrew Thompson void 2288a593f6b8SAndrew Thompson usb_free_device(struct usb_device *udev, uint8_t flag) 228902ac6454SAndrew Thompson { 2290760bc48eSAndrew Thompson struct usb_bus *bus; 2291bdd41206SAndrew Thompson 2292bdd41206SAndrew Thompson if (udev == NULL) 2293bdd41206SAndrew Thompson return; /* already freed */ 229402ac6454SAndrew Thompson 229502ac6454SAndrew Thompson DPRINTFN(4, "udev=%p port=%d\n", udev, udev->port_no); 229602ac6454SAndrew Thompson 2297bd216778SAndrew Thompson bus = udev->bus; 2298f97da128SHans Petter Selasky 2299f97da128SHans Petter Selasky /* set DETACHED state to prevent any further references */ 2300a593f6b8SAndrew Thompson usb_set_device_state(udev, USB_STATE_DETACHED); 2301bdd41206SAndrew Thompson 23028bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 23033acd904dSAndrew Thompson usb_notify_addq("DETACH", udev); 23048bb77249SHans Petter Selasky #endif 230502ac6454SAndrew Thompson 23068bb77249SHans Petter Selasky #if USB_HAVE_UGEN 2307cabe79d9SMarius Strobl if (!rebooting) { 230888f9a4ffSEdward Tomasz Napierala printf("%s: <%s %s> at %s (disconnected)\n", udev->ugen_name, 230988f9a4ffSEdward Tomasz Napierala usb_get_manufacturer(udev), usb_get_product(udev), 231088f9a4ffSEdward Tomasz Napierala device_get_nameunit(bus->bdev)); 2311cabe79d9SMarius Strobl } 231202ac6454SAndrew Thompson 2313ee3e3ff5SAndrew Thompson /* Destroy UGEN symlink, if any */ 231402ac6454SAndrew Thompson if (udev->ugen_symlink) { 2315a593f6b8SAndrew Thompson usb_free_symlink(udev->ugen_symlink); 231602ac6454SAndrew Thompson udev->ugen_symlink = NULL; 231702ac6454SAndrew Thompson } 231802ac6454SAndrew Thompson 23192ffd5fdcSHans Petter Selasky usb_destroy_dev(udev->ctrl_dev); 23200f6e8f93SAndrew Thompson #endif 232102ac6454SAndrew Thompson 2322f29a0724SAndrew Thompson if (udev->flags.usb_mode == USB_MODE_DEVICE) { 232302ac6454SAndrew Thompson /* stop receiving any control transfers (Device Side Mode) */ 23245b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 232502ac6454SAndrew Thompson } 232602ac6454SAndrew Thompson 2327bdd41206SAndrew Thompson /* the following will get the device unconfigured in software */ 2328d88688c7SAndrew Thompson usb_unconfigure(udev, USB_UNCFG_FLAG_FREE_EP0); 232902ac6454SAndrew Thompson 2330f97da128SHans Petter Selasky /* final device unregister after all character devices are closed */ 2331f97da128SHans Petter Selasky usb_bus_port_set_device(bus, udev->parent_hub ? 2332f97da128SHans Petter Selasky udev->parent_hub->hub->ports + udev->port_index : NULL, 2333f97da128SHans Petter Selasky NULL, USB_ROOT_HUB_ADDR); 2334f97da128SHans Petter Selasky 233502ac6454SAndrew Thompson /* unsetup any leftover default USB transfers */ 23365b3bb704SAndrew Thompson usbd_transfer_unsetup(udev->ctrl_xfer, USB_CTRL_XFER_MAX); 233702ac6454SAndrew Thompson 233802ac6454SAndrew Thompson /* template unsetup, if any */ 2339a593f6b8SAndrew Thompson (usb_temp_unsetup_p) (udev); 234002ac6454SAndrew Thompson 234102ac6454SAndrew Thompson /* 234202ac6454SAndrew Thompson * Make sure that our clear-stall messages are not queued 234302ac6454SAndrew Thompson * anywhere: 234402ac6454SAndrew Thompson */ 234502ac6454SAndrew Thompson USB_BUS_LOCK(udev->bus); 234643ea03d7SHans Petter Selasky usb_proc_mwait(USB_BUS_CS_PROC(udev->bus), 234702ac6454SAndrew Thompson &udev->cs_msg[0], &udev->cs_msg[1]); 234802ac6454SAndrew Thompson USB_BUS_UNLOCK(udev->bus); 234902ac6454SAndrew Thompson 2350b78e84d1SHans Petter Selasky /* wait for all references to go away */ 2351b78e84d1SHans Petter Selasky usb_wait_pending_refs(udev); 2352b78e84d1SHans Petter Selasky 235391cd9240SAndrew Thompson sx_destroy(&udev->enum_sx); 23542df1e9a6SAndrew Thompson sx_destroy(&udev->sr_sx); 235564cb5e2aSHans Petter Selasky sx_destroy(&udev->ctrl_sx); 235602ac6454SAndrew Thompson 235791cd9240SAndrew Thompson cv_destroy(&udev->ctrlreq_cv); 235891cd9240SAndrew Thompson cv_destroy(&udev->ref_cv); 235902ac6454SAndrew Thompson 236091cd9240SAndrew Thompson mtx_destroy(&udev->device_mtx); 23618755859aSAndrew Thompson #if USB_HAVE_UGEN 2362ee3e3ff5SAndrew Thompson KASSERT(LIST_FIRST(&udev->pd_list) == NULL, ("leaked cdev entries")); 23638755859aSAndrew Thompson #endif 236402ac6454SAndrew Thompson 2365963169b4SHans Petter Selasky /* Uninitialise device */ 2366963169b4SHans Petter Selasky if (bus->methods->device_uninit != NULL) 2367963169b4SHans Petter Selasky (bus->methods->device_uninit) (udev); 2368963169b4SHans Petter Selasky 236902ac6454SAndrew Thompson /* free device */ 237028212de2SAndrew Thompson free(udev->serial, M_USB); 237128212de2SAndrew Thompson free(udev->manufacturer, M_USB); 237228212de2SAndrew Thompson free(udev->product, M_USB); 237302ac6454SAndrew Thompson free(udev, M_USB); 237402ac6454SAndrew Thompson } 237502ac6454SAndrew Thompson 237602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2377a593f6b8SAndrew Thompson * usbd_get_iface 237802ac6454SAndrew Thompson * 237902ac6454SAndrew Thompson * This function is the safe way to get the USB interface structure 238002ac6454SAndrew Thompson * pointer by interface index. 238102ac6454SAndrew Thompson * 238202ac6454SAndrew Thompson * Return values: 238302ac6454SAndrew Thompson * NULL: Interface not present. 238402ac6454SAndrew Thompson * Else: Pointer to USB interface structure. 238502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 2386760bc48eSAndrew Thompson struct usb_interface * 2387a593f6b8SAndrew Thompson usbd_get_iface(struct usb_device *udev, uint8_t iface_index) 238802ac6454SAndrew Thompson { 2389760bc48eSAndrew Thompson struct usb_interface *iface = udev->ifaces + iface_index; 239002ac6454SAndrew Thompson 2391bdd41206SAndrew Thompson if (iface_index >= udev->ifaces_max) 239202ac6454SAndrew Thompson return (NULL); 239302ac6454SAndrew Thompson return (iface); 239402ac6454SAndrew Thompson } 239502ac6454SAndrew Thompson 239602ac6454SAndrew Thompson /*------------------------------------------------------------------------* 23973c8d24f4SAndrew Thompson * usbd_find_descriptor 239802ac6454SAndrew Thompson * 239902ac6454SAndrew Thompson * This function will lookup the first descriptor that matches the 240002ac6454SAndrew Thompson * criteria given by the arguments "type" and "subtype". Descriptors 240102ac6454SAndrew Thompson * will only be searched within the interface having the index 240202ac6454SAndrew Thompson * "iface_index". If the "id" argument points to an USB descriptor, 240302ac6454SAndrew Thompson * it will be skipped before the search is started. This allows 240402ac6454SAndrew Thompson * searching for multiple descriptors using the same criteria. Else 240502ac6454SAndrew Thompson * the search is started after the interface descriptor. 240602ac6454SAndrew Thompson * 240702ac6454SAndrew Thompson * Return values: 240802ac6454SAndrew Thompson * NULL: End of descriptors 240902ac6454SAndrew Thompson * Else: A descriptor matching the criteria 241002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 241102ac6454SAndrew Thompson void * 24123c8d24f4SAndrew Thompson usbd_find_descriptor(struct usb_device *udev, void *id, uint8_t iface_index, 241302ac6454SAndrew Thompson uint8_t type, uint8_t type_mask, 241402ac6454SAndrew Thompson uint8_t subtype, uint8_t subtype_mask) 241502ac6454SAndrew Thompson { 2416760bc48eSAndrew Thompson struct usb_descriptor *desc; 2417760bc48eSAndrew Thompson struct usb_config_descriptor *cd; 2418760bc48eSAndrew Thompson struct usb_interface *iface; 241902ac6454SAndrew Thompson 2420a593f6b8SAndrew Thompson cd = usbd_get_config_descriptor(udev); 242102ac6454SAndrew Thompson if (cd == NULL) { 242202ac6454SAndrew Thompson return (NULL); 242302ac6454SAndrew Thompson } 242402ac6454SAndrew Thompson if (id == NULL) { 2425a593f6b8SAndrew Thompson iface = usbd_get_iface(udev, iface_index); 242602ac6454SAndrew Thompson if (iface == NULL) { 242702ac6454SAndrew Thompson return (NULL); 242802ac6454SAndrew Thompson } 2429a593f6b8SAndrew Thompson id = usbd_get_interface_descriptor(iface); 243002ac6454SAndrew Thompson if (id == NULL) { 243102ac6454SAndrew Thompson return (NULL); 243202ac6454SAndrew Thompson } 243302ac6454SAndrew Thompson } 243402ac6454SAndrew Thompson desc = (void *)id; 243502ac6454SAndrew Thompson 2436a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 243702ac6454SAndrew Thompson if (desc->bDescriptorType == UDESC_INTERFACE) { 243802ac6454SAndrew Thompson break; 243902ac6454SAndrew Thompson } 244002ac6454SAndrew Thompson if (((desc->bDescriptorType & type_mask) == type) && 244102ac6454SAndrew Thompson ((desc->bDescriptorSubtype & subtype_mask) == subtype)) { 244202ac6454SAndrew Thompson return (desc); 244302ac6454SAndrew Thompson } 244402ac6454SAndrew Thompson } 244502ac6454SAndrew Thompson return (NULL); 244602ac6454SAndrew Thompson } 244702ac6454SAndrew Thompson 244802ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2449a593f6b8SAndrew Thompson * usb_devinfo 245002ac6454SAndrew Thompson * 245102ac6454SAndrew Thompson * This function will dump information from the device descriptor 245202ac6454SAndrew Thompson * belonging to the USB device pointed to by "udev", to the string 245302ac6454SAndrew Thompson * pointed to by "dst_ptr" having a maximum length of "dst_len" bytes 245402ac6454SAndrew Thompson * including the terminating zero. 245502ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 245602ac6454SAndrew Thompson void 2457a593f6b8SAndrew Thompson usb_devinfo(struct usb_device *udev, char *dst_ptr, uint16_t dst_len) 245802ac6454SAndrew Thompson { 2459760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 246002ac6454SAndrew Thompson uint16_t bcdDevice; 246102ac6454SAndrew Thompson uint16_t bcdUSB; 246202ac6454SAndrew Thompson 246302ac6454SAndrew Thompson bcdUSB = UGETW(udd->bcdUSB); 246402ac6454SAndrew Thompson bcdDevice = UGETW(udd->bcdDevice); 246502ac6454SAndrew Thompson 246602ac6454SAndrew Thompson if (udd->bDeviceClass != 0xFF) { 246702ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, class %d/%d, rev %x.%02x/" 24688755859aSAndrew Thompson "%x.%02x, addr %d", 2469ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2470ae538d85SAndrew Thompson usb_get_product(udev), 247102ac6454SAndrew Thompson udd->bDeviceClass, udd->bDeviceSubClass, 247202ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 247302ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 247402ac6454SAndrew Thompson udev->address); 247502ac6454SAndrew Thompson } else { 247602ac6454SAndrew Thompson snprintf(dst_ptr, dst_len, "%s %s, rev %x.%02x/" 24778755859aSAndrew Thompson "%x.%02x, addr %d", 2478ae538d85SAndrew Thompson usb_get_manufacturer(udev), 2479ae538d85SAndrew Thompson usb_get_product(udev), 248002ac6454SAndrew Thompson (bcdUSB >> 8), bcdUSB & 0xFF, 248102ac6454SAndrew Thompson (bcdDevice >> 8), bcdDevice & 0xFF, 248202ac6454SAndrew Thompson udev->address); 248302ac6454SAndrew Thompson } 248402ac6454SAndrew Thompson } 248502ac6454SAndrew Thompson 2486ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 248702ac6454SAndrew Thompson /* 248802ac6454SAndrew Thompson * Descriptions of of known vendors and devices ("products"). 248902ac6454SAndrew Thompson */ 249002ac6454SAndrew Thompson struct usb_knowndev { 249102ac6454SAndrew Thompson uint16_t vendor; 249202ac6454SAndrew Thompson uint16_t product; 249302ac6454SAndrew Thompson uint32_t flags; 249402ac6454SAndrew Thompson const char *vendorname; 249502ac6454SAndrew Thompson const char *productname; 249602ac6454SAndrew Thompson }; 249702ac6454SAndrew Thompson 249802ac6454SAndrew Thompson #define USB_KNOWNDEV_NOPROD 0x01 /* match on vendor only */ 249902ac6454SAndrew Thompson 250002ac6454SAndrew Thompson #include "usbdevs.h" 250102ac6454SAndrew Thompson #include "usbdevs_data.h" 250202ac6454SAndrew Thompson #endif /* USB_VERBOSE */ 250302ac6454SAndrew Thompson 2504f54ab96dSHans Petter Selasky void 2505f54ab96dSHans Petter Selasky usb_set_device_strings(struct usb_device *udev) 250602ac6454SAndrew Thompson { 2507760bc48eSAndrew Thompson struct usb_device_descriptor *udd = &udev->ddesc; 2508ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 250902ac6454SAndrew Thompson const struct usb_knowndev *kdp; 251002ac6454SAndrew Thompson #endif 25112df1e9a6SAndrew Thompson char *temp_ptr; 25127a918edfSAndrew Thompson size_t temp_size; 251302ac6454SAndrew Thompson uint16_t vendor_id; 251402ac6454SAndrew Thompson uint16_t product_id; 25156950c75fSHans Petter Selasky uint8_t do_unlock; 251602ac6454SAndrew Thompson 25176950c75fSHans Petter Selasky /* Protect scratch area */ 251864cb5e2aSHans Petter Selasky do_unlock = usbd_ctrl_lock(udev); 25196950c75fSHans Petter Selasky 25206950c75fSHans Petter Selasky temp_ptr = (char *)udev->scratch.data; 25216950c75fSHans Petter Selasky temp_size = sizeof(udev->scratch.data); 25227a918edfSAndrew Thompson 252302ac6454SAndrew Thompson vendor_id = UGETW(udd->idVendor); 252402ac6454SAndrew Thompson product_id = UGETW(udd->idProduct); 252502ac6454SAndrew Thompson 2526f54ab96dSHans Petter Selasky /* cleanup old strings, if any */ 2527f54ab96dSHans Petter Selasky free(udev->serial, M_USB); 2528f54ab96dSHans Petter Selasky free(udev->manufacturer, M_USB); 2529f54ab96dSHans Petter Selasky free(udev->product, M_USB); 2530f54ab96dSHans Petter Selasky 2531f54ab96dSHans Petter Selasky /* zero the string pointers */ 2532f54ab96dSHans Petter Selasky udev->serial = NULL; 2533f54ab96dSHans Petter Selasky udev->manufacturer = NULL; 2534f54ab96dSHans Petter Selasky udev->product = NULL; 2535f54ab96dSHans Petter Selasky 2536c86aead6SAndrew Thompson /* get serial number string */ 25377a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2538c86aead6SAndrew Thompson udev->ddesc.iSerialNumber); 25397a918edfSAndrew Thompson udev->serial = strdup(temp_ptr, M_USB); 254002ac6454SAndrew Thompson 2541c86aead6SAndrew Thompson /* get manufacturer string */ 25427a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2543c86aead6SAndrew Thompson udev->ddesc.iManufacturer); 25447a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 25457a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 25467a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 2547c86aead6SAndrew Thompson 2548c86aead6SAndrew Thompson /* get product string */ 25497a918edfSAndrew Thompson usbd_req_get_string_any(udev, NULL, temp_ptr, temp_size, 2550c86aead6SAndrew Thompson udev->ddesc.iProduct); 25517a918edfSAndrew Thompson usb_trim_spaces(temp_ptr); 25527a918edfSAndrew Thompson if (temp_ptr[0] != '\0') 25537a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 2554c86aead6SAndrew Thompson 2555ed6d949aSAndrew Thompson #ifdef USB_VERBOSE 2556c86aead6SAndrew Thompson if (udev->manufacturer == NULL || udev->product == NULL) { 2557c86aead6SAndrew Thompson for (kdp = usb_knowndevs; kdp->vendorname != NULL; kdp++) { 255802ac6454SAndrew Thompson if (kdp->vendor == vendor_id && 255902ac6454SAndrew Thompson (kdp->product == product_id || 256002ac6454SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) != 0)) 256102ac6454SAndrew Thompson break; 256202ac6454SAndrew Thompson } 256302ac6454SAndrew Thompson if (kdp->vendorname != NULL) { 2564c86aead6SAndrew Thompson /* XXX should use pointer to knowndevs string */ 2565c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 2566c86aead6SAndrew Thompson udev->manufacturer = strdup(kdp->vendorname, 2567c86aead6SAndrew Thompson M_USB); 2568c86aead6SAndrew Thompson } 2569c86aead6SAndrew Thompson if (udev->product == NULL && 2570c86aead6SAndrew Thompson (kdp->flags & USB_KNOWNDEV_NOPROD) == 0) { 2571c86aead6SAndrew Thompson udev->product = strdup(kdp->productname, 2572c86aead6SAndrew Thompson M_USB); 2573c86aead6SAndrew Thompson } 257402ac6454SAndrew Thompson } 257502ac6454SAndrew Thompson } 257602ac6454SAndrew Thompson #endif 2577c86aead6SAndrew Thompson /* Provide default strings if none were found */ 2578c86aead6SAndrew Thompson if (udev->manufacturer == NULL) { 25797a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "vendor 0x%04x", vendor_id); 25807a918edfSAndrew Thompson udev->manufacturer = strdup(temp_ptr, M_USB); 258102ac6454SAndrew Thompson } 2582c86aead6SAndrew Thompson if (udev->product == NULL) { 25837a918edfSAndrew Thompson snprintf(temp_ptr, temp_size, "product 0x%04x", product_id); 25847a918edfSAndrew Thompson udev->product = strdup(temp_ptr, M_USB); 258502ac6454SAndrew Thompson } 25866950c75fSHans Petter Selasky 25876950c75fSHans Petter Selasky if (do_unlock) 258864cb5e2aSHans Petter Selasky usbd_ctrl_unlock(udev); 258902ac6454SAndrew Thompson } 259002ac6454SAndrew Thompson 259102ac6454SAndrew Thompson /* 259202ac6454SAndrew Thompson * Returns: 259302ac6454SAndrew Thompson * See: USB_MODE_XXX 259402ac6454SAndrew Thompson */ 2595f29a0724SAndrew Thompson enum usb_hc_mode 2596a593f6b8SAndrew Thompson usbd_get_mode(struct usb_device *udev) 259702ac6454SAndrew Thompson { 2598f29a0724SAndrew Thompson return (udev->flags.usb_mode); 259902ac6454SAndrew Thompson } 260002ac6454SAndrew Thompson 260102ac6454SAndrew Thompson /* 260202ac6454SAndrew Thompson * Returns: 260302ac6454SAndrew Thompson * See: USB_SPEED_XXX 260402ac6454SAndrew Thompson */ 26058d2dd5ddSAndrew Thompson enum usb_dev_speed 2606a593f6b8SAndrew Thompson usbd_get_speed(struct usb_device *udev) 260702ac6454SAndrew Thompson { 260802ac6454SAndrew Thompson return (udev->speed); 260902ac6454SAndrew Thompson } 261002ac6454SAndrew Thompson 261102ac6454SAndrew Thompson uint32_t 2612a593f6b8SAndrew Thompson usbd_get_isoc_fps(struct usb_device *udev) 261302ac6454SAndrew Thompson { 261402ac6454SAndrew Thompson ; /* indent fix */ 261502ac6454SAndrew Thompson switch (udev->speed) { 261602ac6454SAndrew Thompson case USB_SPEED_LOW: 261702ac6454SAndrew Thompson case USB_SPEED_FULL: 261802ac6454SAndrew Thompson return (1000); 261902ac6454SAndrew Thompson default: 262002ac6454SAndrew Thompson return (8000); 262102ac6454SAndrew Thompson } 262202ac6454SAndrew Thompson } 262302ac6454SAndrew Thompson 2624760bc48eSAndrew Thompson struct usb_device_descriptor * 2625a593f6b8SAndrew Thompson usbd_get_device_descriptor(struct usb_device *udev) 262602ac6454SAndrew Thompson { 262702ac6454SAndrew Thompson if (udev == NULL) 262802ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 262902ac6454SAndrew Thompson return (&udev->ddesc); 263002ac6454SAndrew Thompson } 263102ac6454SAndrew Thompson 2632760bc48eSAndrew Thompson struct usb_config_descriptor * 2633a593f6b8SAndrew Thompson usbd_get_config_descriptor(struct usb_device *udev) 263402ac6454SAndrew Thompson { 263502ac6454SAndrew Thompson if (udev == NULL) 263602ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 263702ac6454SAndrew Thompson return (udev->cdesc); 263802ac6454SAndrew Thompson } 263902ac6454SAndrew Thompson 264002ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2641a593f6b8SAndrew Thompson * usb_test_quirk - test a device for a given quirk 264202ac6454SAndrew Thompson * 264302ac6454SAndrew Thompson * Return values: 264402ac6454SAndrew Thompson * 0: The USB device does not have the given quirk. 264502ac6454SAndrew Thompson * Else: The USB device has the given quirk. 264602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 264702ac6454SAndrew Thompson uint8_t 2648a593f6b8SAndrew Thompson usb_test_quirk(const struct usb_attach_arg *uaa, uint16_t quirk) 264902ac6454SAndrew Thompson { 265002ac6454SAndrew Thompson uint8_t found; 26513f37fb62SHans Petter Selasky uint8_t x; 26523f37fb62SHans Petter Selasky 26533f37fb62SHans Petter Selasky if (quirk == UQ_NONE) 26543f37fb62SHans Petter Selasky return (0); 26553f37fb62SHans Petter Selasky 26563f37fb62SHans Petter Selasky /* search the automatic per device quirks first */ 26573f37fb62SHans Petter Selasky 26583f37fb62SHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 26593f37fb62SHans Petter Selasky if (uaa->device->autoQuirk[x] == quirk) 26603f37fb62SHans Petter Selasky return (1); 26613f37fb62SHans Petter Selasky } 26623f37fb62SHans Petter Selasky 26633f37fb62SHans Petter Selasky /* search global quirk table, if any */ 266402ac6454SAndrew Thompson 2665a593f6b8SAndrew Thompson found = (usb_test_quirk_p) (&uaa->info, quirk); 26663f37fb62SHans Petter Selasky 266702ac6454SAndrew Thompson return (found); 266802ac6454SAndrew Thompson } 266902ac6454SAndrew Thompson 2670760bc48eSAndrew Thompson struct usb_interface_descriptor * 2671a593f6b8SAndrew Thompson usbd_get_interface_descriptor(struct usb_interface *iface) 267202ac6454SAndrew Thompson { 267302ac6454SAndrew Thompson if (iface == NULL) 267402ac6454SAndrew Thompson return (NULL); /* be NULL safe */ 267502ac6454SAndrew Thompson return (iface->idesc); 267602ac6454SAndrew Thompson } 267702ac6454SAndrew Thompson 267802ac6454SAndrew Thompson uint8_t 2679a593f6b8SAndrew Thompson usbd_get_interface_altindex(struct usb_interface *iface) 268002ac6454SAndrew Thompson { 268102ac6454SAndrew Thompson return (iface->alt_index); 268202ac6454SAndrew Thompson } 268302ac6454SAndrew Thompson 268402ac6454SAndrew Thompson uint8_t 2685a593f6b8SAndrew Thompson usbd_get_bus_index(struct usb_device *udev) 268602ac6454SAndrew Thompson { 268702ac6454SAndrew Thompson return ((uint8_t)device_get_unit(udev->bus->bdev)); 268802ac6454SAndrew Thompson } 268902ac6454SAndrew Thompson 269002ac6454SAndrew Thompson uint8_t 2691a593f6b8SAndrew Thompson usbd_get_device_index(struct usb_device *udev) 269202ac6454SAndrew Thompson { 269302ac6454SAndrew Thompson return (udev->device_index); 269402ac6454SAndrew Thompson } 269502ac6454SAndrew Thompson 26968bb77249SHans Petter Selasky #if USB_HAVE_DEVCTL 26973acd904dSAndrew Thompson static void 26983acd904dSAndrew Thompson usb_notify_addq(const char *type, struct usb_device *udev) 26993acd904dSAndrew Thompson { 27003acd904dSAndrew Thompson struct usb_interface *iface; 27013acd904dSAndrew Thompson struct sbuf *sb; 27023acd904dSAndrew Thompson int i; 27033acd904dSAndrew Thompson 27043acd904dSAndrew Thompson /* announce the device */ 27053acd904dSAndrew Thompson sb = sbuf_new_auto(); 27063acd904dSAndrew Thompson sbuf_printf(sb, 27078bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27088bb77249SHans Petter Selasky "ugen=%s " 270902c40241SHans Petter Selasky "cdev=%s " 27108bb77249SHans Petter Selasky #endif 27113acd904dSAndrew Thompson "vendor=0x%04x " 27123acd904dSAndrew Thompson "product=0x%04x " 27133acd904dSAndrew Thompson "devclass=0x%02x " 27143acd904dSAndrew Thompson "devsubclass=0x%02x " 27153acd904dSAndrew Thompson "sernum=\"%s\" " 27163acd904dSAndrew Thompson "release=0x%04x " 271766390216SAndrew Thompson "mode=%s " 27183acd904dSAndrew Thompson "port=%u " 27198bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27206df3eebbSNick Hibma "parent=%s" 27218bb77249SHans Petter Selasky #endif 27228bb77249SHans Petter Selasky "", 27238bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27243acd904dSAndrew Thompson udev->ugen_name, 272502c40241SHans Petter Selasky udev->ugen_name, 27268bb77249SHans Petter Selasky #endif 27273acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 27283acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 27293acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 27303acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2731ae538d85SAndrew Thompson usb_get_serial(udev), 27323acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 273366390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 27348bb77249SHans Petter Selasky udev->port_no 27358bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27368bb77249SHans Petter Selasky , udev->parent_hub != NULL ? 27373acd904dSAndrew Thompson udev->parent_hub->ugen_name : 27388bb77249SHans Petter Selasky device_get_nameunit(device_get_parent(udev->bus->bdev)) 27398bb77249SHans Petter Selasky #endif 27408bb77249SHans Petter Selasky ); 27413acd904dSAndrew Thompson sbuf_finish(sb); 27423acd904dSAndrew Thompson devctl_notify("USB", "DEVICE", type, sbuf_data(sb)); 27433acd904dSAndrew Thompson sbuf_delete(sb); 27443acd904dSAndrew Thompson 27453acd904dSAndrew Thompson /* announce each interface */ 27463acd904dSAndrew Thompson for (i = 0; i < USB_IFACE_MAX; i++) { 27473acd904dSAndrew Thompson iface = usbd_get_iface(udev, i); 27483acd904dSAndrew Thompson if (iface == NULL) 27493acd904dSAndrew Thompson break; /* end of interfaces */ 27503acd904dSAndrew Thompson if (iface->idesc == NULL) 27513acd904dSAndrew Thompson continue; /* no interface descriptor */ 27523acd904dSAndrew Thompson 27533acd904dSAndrew Thompson sb = sbuf_new_auto(); 27543acd904dSAndrew Thompson sbuf_printf(sb, 27558bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27568bb77249SHans Petter Selasky "ugen=%s " 275702c40241SHans Petter Selasky "cdev=%s " 27588bb77249SHans Petter Selasky #endif 27593acd904dSAndrew Thompson "vendor=0x%04x " 27603acd904dSAndrew Thompson "product=0x%04x " 27613acd904dSAndrew Thompson "devclass=0x%02x " 27623acd904dSAndrew Thompson "devsubclass=0x%02x " 27633acd904dSAndrew Thompson "sernum=\"%s\" " 27643acd904dSAndrew Thompson "release=0x%04x " 276566390216SAndrew Thompson "mode=%s " 27663acd904dSAndrew Thompson "interface=%d " 27673acd904dSAndrew Thompson "endpoints=%d " 27683acd904dSAndrew Thompson "intclass=0x%02x " 27693acd904dSAndrew Thompson "intsubclass=0x%02x " 27706df3eebbSNick Hibma "intprotocol=0x%02x", 27718bb77249SHans Petter Selasky #if USB_HAVE_UGEN 27723acd904dSAndrew Thompson udev->ugen_name, 277302c40241SHans Petter Selasky udev->ugen_name, 27748bb77249SHans Petter Selasky #endif 27753acd904dSAndrew Thompson UGETW(udev->ddesc.idVendor), 27763acd904dSAndrew Thompson UGETW(udev->ddesc.idProduct), 27773acd904dSAndrew Thompson udev->ddesc.bDeviceClass, 27783acd904dSAndrew Thompson udev->ddesc.bDeviceSubClass, 2779ae538d85SAndrew Thompson usb_get_serial(udev), 27803acd904dSAndrew Thompson UGETW(udev->ddesc.bcdDevice), 278166390216SAndrew Thompson (udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device", 27823acd904dSAndrew Thompson iface->idesc->bInterfaceNumber, 27833acd904dSAndrew Thompson iface->idesc->bNumEndpoints, 27843acd904dSAndrew Thompson iface->idesc->bInterfaceClass, 27853acd904dSAndrew Thompson iface->idesc->bInterfaceSubClass, 27863acd904dSAndrew Thompson iface->idesc->bInterfaceProtocol); 27873acd904dSAndrew Thompson sbuf_finish(sb); 27883acd904dSAndrew Thompson devctl_notify("USB", "INTERFACE", type, sbuf_data(sb)); 27893acd904dSAndrew Thompson sbuf_delete(sb); 27903acd904dSAndrew Thompson } 27913acd904dSAndrew Thompson } 27928bb77249SHans Petter Selasky #endif 279302ac6454SAndrew Thompson 27948bb77249SHans Petter Selasky #if USB_HAVE_UGEN 279502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2796a593f6b8SAndrew Thompson * usb_fifo_free_wrap 279702ac6454SAndrew Thompson * 279802ac6454SAndrew Thompson * This function will free the FIFOs. 279902ac6454SAndrew Thompson * 2800bdd41206SAndrew Thompson * Description of "flag" argument: If the USB_UNCFG_FLAG_FREE_EP0 flag 2801bdd41206SAndrew Thompson * is set and "iface_index" is set to "USB_IFACE_INDEX_ANY", we free 2802bdd41206SAndrew Thompson * all FIFOs. If the USB_UNCFG_FLAG_FREE_EP0 flag is not set and 2803bdd41206SAndrew Thompson * "iface_index" is set to "USB_IFACE_INDEX_ANY", we free all non 2804bdd41206SAndrew Thompson * control endpoint FIFOs. If "iface_index" is not set to 2805bdd41206SAndrew Thompson * "USB_IFACE_INDEX_ANY" the flag has no effect. 280602ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 280702ac6454SAndrew Thompson static void 2808a593f6b8SAndrew Thompson usb_fifo_free_wrap(struct usb_device *udev, 280902ac6454SAndrew Thompson uint8_t iface_index, uint8_t flag) 281002ac6454SAndrew Thompson { 2811760bc48eSAndrew Thompson struct usb_fifo *f; 281202ac6454SAndrew Thompson uint16_t i; 281302ac6454SAndrew Thompson 281402ac6454SAndrew Thompson /* 281502ac6454SAndrew Thompson * Free any USB FIFOs on the given interface: 281602ac6454SAndrew Thompson */ 281702ac6454SAndrew Thompson for (i = 0; i != USB_FIFO_MAX; i++) { 281802ac6454SAndrew Thompson f = udev->fifo[i]; 281902ac6454SAndrew Thompson if (f == NULL) { 282002ac6454SAndrew Thompson continue; 282102ac6454SAndrew Thompson } 282202ac6454SAndrew Thompson /* Check if the interface index matches */ 282302ac6454SAndrew Thompson if (iface_index == f->iface_index) { 2824a593f6b8SAndrew Thompson if (f->methods != &usb_ugen_methods) { 282502ac6454SAndrew Thompson /* 282602ac6454SAndrew Thompson * Don't free any non-generic FIFOs in 282702ac6454SAndrew Thompson * this case. 282802ac6454SAndrew Thompson */ 282902ac6454SAndrew Thompson continue; 283002ac6454SAndrew Thompson } 283102ac6454SAndrew Thompson if ((f->dev_ep_index == 0) && 2832*9b077d72SHans Petter Selasky (f->fs_ep_max == 0)) { 283302ac6454SAndrew Thompson /* no need to free this FIFO */ 283402ac6454SAndrew Thompson continue; 283502ac6454SAndrew Thompson } 283602ac6454SAndrew Thompson } else if (iface_index == USB_IFACE_INDEX_ANY) { 2837a593f6b8SAndrew Thompson if ((f->methods == &usb_ugen_methods) && 2838bdd41206SAndrew Thompson (f->dev_ep_index == 0) && 2839bdd41206SAndrew Thompson (!(flag & USB_UNCFG_FLAG_FREE_EP0)) && 2840*9b077d72SHans Petter Selasky (f->fs_ep_max == 0)) { 284102ac6454SAndrew Thompson /* no need to free this FIFO */ 284202ac6454SAndrew Thompson continue; 284302ac6454SAndrew Thompson } 284402ac6454SAndrew Thompson } else { 284502ac6454SAndrew Thompson /* no need to free this FIFO */ 284602ac6454SAndrew Thompson continue; 284702ac6454SAndrew Thompson } 284802ac6454SAndrew Thompson /* free this FIFO */ 2849a593f6b8SAndrew Thompson usb_fifo_free(f); 285002ac6454SAndrew Thompson } 285102ac6454SAndrew Thompson } 28520f6e8f93SAndrew Thompson #endif 285302ac6454SAndrew Thompson 285402ac6454SAndrew Thompson /*------------------------------------------------------------------------* 2855a593f6b8SAndrew Thompson * usb_peer_can_wakeup 285602ac6454SAndrew Thompson * 285702ac6454SAndrew Thompson * Return values: 285802ac6454SAndrew Thompson * 0: Peer cannot do resume signalling. 285902ac6454SAndrew Thompson * Else: Peer can do resume signalling. 286002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 286102ac6454SAndrew Thompson uint8_t 2862a593f6b8SAndrew Thompson usb_peer_can_wakeup(struct usb_device *udev) 286302ac6454SAndrew Thompson { 2864760bc48eSAndrew Thompson const struct usb_config_descriptor *cdp; 286502ac6454SAndrew Thompson 286602ac6454SAndrew Thompson cdp = udev->cdesc; 2867f29a0724SAndrew Thompson if ((cdp != NULL) && (udev->flags.usb_mode == USB_MODE_HOST)) { 286802ac6454SAndrew Thompson return (cdp->bmAttributes & UC_REMOTE_WAKEUP); 286902ac6454SAndrew Thompson } 287002ac6454SAndrew Thompson return (0); /* not supported */ 287102ac6454SAndrew Thompson } 2872bd216778SAndrew Thompson 2873bd216778SAndrew Thompson void 2874a593f6b8SAndrew Thompson usb_set_device_state(struct usb_device *udev, enum usb_dev_state state) 2875bd216778SAndrew Thompson { 2876bd216778SAndrew Thompson 2877bd216778SAndrew Thompson KASSERT(state < USB_STATE_MAX, ("invalid udev state")); 2878bd216778SAndrew Thompson 2879bd216778SAndrew Thompson DPRINTF("udev %p state %s -> %s\n", udev, 2880a593f6b8SAndrew Thompson usb_statestr(udev->state), usb_statestr(state)); 2881963169b4SHans Petter Selasky 2882f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2883f97da128SHans Petter Selasky mtx_lock(&usb_ref_lock); 2884f97da128SHans Petter Selasky #endif 2885f97da128SHans Petter Selasky udev->state = state; 2886f97da128SHans Petter Selasky #if USB_HAVE_UGEN 2887f97da128SHans Petter Selasky mtx_unlock(&usb_ref_lock); 2888f97da128SHans Petter Selasky #endif 2889963169b4SHans Petter Selasky if (udev->bus->methods->device_state_change != NULL) 2890963169b4SHans Petter Selasky (udev->bus->methods->device_state_change) (udev); 2891963169b4SHans Petter Selasky } 2892963169b4SHans Petter Selasky 2893963169b4SHans Petter Selasky enum usb_dev_state 2894963169b4SHans Petter Selasky usb_get_device_state(struct usb_device *udev) 2895963169b4SHans Petter Selasky { 2896963169b4SHans Petter Selasky if (udev == NULL) 2897963169b4SHans Petter Selasky return (USB_STATE_DETACHED); 2898963169b4SHans Petter Selasky return (udev->state); 2899bd216778SAndrew Thompson } 2900bd216778SAndrew Thompson 2901ec8f3127SAndrew Thompson uint8_t 2902a593f6b8SAndrew Thompson usbd_device_attached(struct usb_device *udev) 2903bd216778SAndrew Thompson { 2904bd216778SAndrew Thompson return (udev->state > USB_STATE_DETACHED); 2905bd216778SAndrew Thompson } 2906cb18f7d1SAlfred Perlstein 29076950c75fSHans Petter Selasky /* 29086950c75fSHans Petter Selasky * The following function locks enumerating the given USB device. If 29096950c75fSHans Petter Selasky * the lock is already grabbed this function returns zero. Else a 291010aab8b6SHans Petter Selasky * a value of one is returned. 29116950c75fSHans Petter Selasky */ 29126950c75fSHans Petter Selasky uint8_t 2913cb18f7d1SAlfred Perlstein usbd_enum_lock(struct usb_device *udev) 2914cb18f7d1SAlfred Perlstein { 29156950c75fSHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 29166950c75fSHans Petter Selasky return (0); 29176950c75fSHans Petter Selasky 291891cd9240SAndrew Thompson sx_xlock(&udev->enum_sx); 29192df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 2920cb18f7d1SAlfred Perlstein /* 2921cb18f7d1SAlfred Perlstein * NEWBUS LOCK NOTE: We should check if any parent SX locks 2922cb18f7d1SAlfred Perlstein * are locked before locking Giant. Else the lock can be 2923cb18f7d1SAlfred Perlstein * locked multiple times. 2924cb18f7d1SAlfred Perlstein */ 2925c6df6f53SWarner Losh bus_topo_lock(); 29266950c75fSHans Petter Selasky return (1); 2927cb18f7d1SAlfred Perlstein } 2928cb18f7d1SAlfred Perlstein 292910aab8b6SHans Petter Selasky #if USB_HAVE_UGEN 293010aab8b6SHans Petter Selasky /* 293110aab8b6SHans Petter Selasky * This function is the same like usbd_enum_lock() except a value of 293210aab8b6SHans Petter Selasky * 255 is returned when a signal is pending: 293310aab8b6SHans Petter Selasky */ 293410aab8b6SHans Petter Selasky uint8_t 293510aab8b6SHans Petter Selasky usbd_enum_lock_sig(struct usb_device *udev) 293610aab8b6SHans Petter Selasky { 293710aab8b6SHans Petter Selasky if (sx_xlocked(&udev->enum_sx)) 293810aab8b6SHans Petter Selasky return (0); 293910aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->enum_sx)) 294010aab8b6SHans Petter Selasky return (255); 294110aab8b6SHans Petter Selasky if (sx_xlock_sig(&udev->sr_sx)) { 294210aab8b6SHans Petter Selasky sx_xunlock(&udev->enum_sx); 294310aab8b6SHans Petter Selasky return (255); 294410aab8b6SHans Petter Selasky } 2945c6df6f53SWarner Losh bus_topo_lock(); 294610aab8b6SHans Petter Selasky return (1); 294710aab8b6SHans Petter Selasky } 294810aab8b6SHans Petter Selasky #endif 294910aab8b6SHans Petter Selasky 2950cb18f7d1SAlfred Perlstein /* The following function unlocks enumerating the given USB device. */ 2951cb18f7d1SAlfred Perlstein 2952cb18f7d1SAlfred Perlstein void 2953cb18f7d1SAlfred Perlstein usbd_enum_unlock(struct usb_device *udev) 2954cb18f7d1SAlfred Perlstein { 2955c6df6f53SWarner Losh bus_topo_unlock(); 295691cd9240SAndrew Thompson sx_xunlock(&udev->enum_sx); 29572df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 29582df1e9a6SAndrew Thompson } 29592df1e9a6SAndrew Thompson 29602df1e9a6SAndrew Thompson /* The following function locks suspend and resume. */ 29612df1e9a6SAndrew Thompson 29622df1e9a6SAndrew Thompson void 29632df1e9a6SAndrew Thompson usbd_sr_lock(struct usb_device *udev) 29642df1e9a6SAndrew Thompson { 29652df1e9a6SAndrew Thompson sx_xlock(&udev->sr_sx); 29662df1e9a6SAndrew Thompson /* 29672df1e9a6SAndrew Thompson * NEWBUS LOCK NOTE: We should check if any parent SX locks 29682df1e9a6SAndrew Thompson * are locked before locking Giant. Else the lock can be 29692df1e9a6SAndrew Thompson * locked multiple times. 29702df1e9a6SAndrew Thompson */ 2971c6df6f53SWarner Losh bus_topo_lock(); 29722df1e9a6SAndrew Thompson } 29732df1e9a6SAndrew Thompson 29742df1e9a6SAndrew Thompson /* The following function unlocks suspend and resume. */ 29752df1e9a6SAndrew Thompson 29762df1e9a6SAndrew Thompson void 29772df1e9a6SAndrew Thompson usbd_sr_unlock(struct usb_device *udev) 29782df1e9a6SAndrew Thompson { 2979c6df6f53SWarner Losh bus_topo_unlock(); 29802df1e9a6SAndrew Thompson sx_xunlock(&udev->sr_sx); 2981cb18f7d1SAlfred Perlstein } 2982cb18f7d1SAlfred Perlstein 2983cb18f7d1SAlfred Perlstein /* 2984cb18f7d1SAlfred Perlstein * The following function checks the enumerating lock for the given 2985cb18f7d1SAlfred Perlstein * USB device. 2986cb18f7d1SAlfred Perlstein */ 2987cb18f7d1SAlfred Perlstein 2988cb18f7d1SAlfred Perlstein uint8_t 2989cb18f7d1SAlfred Perlstein usbd_enum_is_locked(struct usb_device *udev) 2990cb18f7d1SAlfred Perlstein { 299191cd9240SAndrew Thompson return (sx_xlocked(&udev->enum_sx)); 2992cb18f7d1SAlfred Perlstein } 29938427ed84SHans Petter Selasky 29948427ed84SHans Petter Selasky /* 299564cb5e2aSHans Petter Selasky * The following function is used to serialize access to USB control 299664cb5e2aSHans Petter Selasky * transfers and the USB scratch area. If the lock is already grabbed 299764cb5e2aSHans Petter Selasky * this function returns zero. Else a value of one is returned. 299864cb5e2aSHans Petter Selasky */ 299964cb5e2aSHans Petter Selasky uint8_t 300064cb5e2aSHans Petter Selasky usbd_ctrl_lock(struct usb_device *udev) 300164cb5e2aSHans Petter Selasky { 300264cb5e2aSHans Petter Selasky if (sx_xlocked(&udev->ctrl_sx)) 300364cb5e2aSHans Petter Selasky return (0); 300464cb5e2aSHans Petter Selasky sx_xlock(&udev->ctrl_sx); 300564cb5e2aSHans Petter Selasky 300664cb5e2aSHans Petter Selasky /* 300764cb5e2aSHans Petter Selasky * We need to allow suspend and resume at this point, else the 300864cb5e2aSHans Petter Selasky * control transfer will timeout if the device is suspended! 300964cb5e2aSHans Petter Selasky */ 301064cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 301164cb5e2aSHans Petter Selasky usbd_sr_unlock(udev); 301264cb5e2aSHans Petter Selasky return (1); 301364cb5e2aSHans Petter Selasky } 301464cb5e2aSHans Petter Selasky 301564cb5e2aSHans Petter Selasky void 301664cb5e2aSHans Petter Selasky usbd_ctrl_unlock(struct usb_device *udev) 301764cb5e2aSHans Petter Selasky { 301864cb5e2aSHans Petter Selasky sx_xunlock(&udev->ctrl_sx); 301964cb5e2aSHans Petter Selasky 302064cb5e2aSHans Petter Selasky /* 302164cb5e2aSHans Petter Selasky * Restore the suspend and resume lock after we have unlocked 302264cb5e2aSHans Petter Selasky * the USB control transfer lock to avoid LOR: 302364cb5e2aSHans Petter Selasky */ 302464cb5e2aSHans Petter Selasky if (usbd_enum_is_locked(udev)) 302564cb5e2aSHans Petter Selasky usbd_sr_lock(udev); 302664cb5e2aSHans Petter Selasky } 302764cb5e2aSHans Petter Selasky 302864cb5e2aSHans Petter Selasky /* 30298427ed84SHans Petter Selasky * The following function is used to set the per-interface specific 30308427ed84SHans Petter Selasky * plug and play information. The string referred to by the pnpinfo 30318427ed84SHans Petter Selasky * argument can safely be freed after calling this function. The 30328427ed84SHans Petter Selasky * pnpinfo of an interface will be reset at device detach or when 30338427ed84SHans Petter Selasky * passing a NULL argument to this function. This function 30348427ed84SHans Petter Selasky * returns zero on success, else a USB_ERR_XXX failure code. 30358427ed84SHans Petter Selasky */ 30368427ed84SHans Petter Selasky 30378427ed84SHans Petter Selasky usb_error_t 30388427ed84SHans Petter Selasky usbd_set_pnpinfo(struct usb_device *udev, uint8_t iface_index, const char *pnpinfo) 30398427ed84SHans Petter Selasky { 30408427ed84SHans Petter Selasky struct usb_interface *iface; 30418427ed84SHans Petter Selasky 30428427ed84SHans Petter Selasky iface = usbd_get_iface(udev, iface_index); 30438427ed84SHans Petter Selasky if (iface == NULL) 30448427ed84SHans Petter Selasky return (USB_ERR_INVAL); 30458427ed84SHans Petter Selasky 30468427ed84SHans Petter Selasky if (iface->pnpinfo != NULL) { 30478427ed84SHans Petter Selasky free(iface->pnpinfo, M_USBDEV); 30488427ed84SHans Petter Selasky iface->pnpinfo = NULL; 30498427ed84SHans Petter Selasky } 30508427ed84SHans Petter Selasky 30518427ed84SHans Petter Selasky if (pnpinfo == NULL || pnpinfo[0] == 0) 30528427ed84SHans Petter Selasky return (0); /* success */ 30538427ed84SHans Petter Selasky 30548427ed84SHans Petter Selasky iface->pnpinfo = strdup(pnpinfo, M_USBDEV); 30558427ed84SHans Petter Selasky if (iface->pnpinfo == NULL) 30568427ed84SHans Petter Selasky return (USB_ERR_NOMEM); 30578427ed84SHans Petter Selasky 30588427ed84SHans Petter Selasky return (0); /* success */ 30598427ed84SHans Petter Selasky } 30608427ed84SHans Petter Selasky 3061d46dc4adSHans Petter Selasky usb_error_t 3062d46dc4adSHans Petter Selasky usbd_add_dynamic_quirk(struct usb_device *udev, uint16_t quirk) 3063d46dc4adSHans Petter Selasky { 3064d46dc4adSHans Petter Selasky uint8_t x; 3065d46dc4adSHans Petter Selasky 3066d46dc4adSHans Petter Selasky for (x = 0; x != USB_MAX_AUTO_QUIRK; x++) { 30673f37fb62SHans Petter Selasky if (udev->autoQuirk[x] == 0 || 30683f37fb62SHans Petter Selasky udev->autoQuirk[x] == quirk) { 3069d46dc4adSHans Petter Selasky udev->autoQuirk[x] = quirk; 3070d46dc4adSHans Petter Selasky return (0); /* success */ 3071d46dc4adSHans Petter Selasky } 3072d46dc4adSHans Petter Selasky } 3073d46dc4adSHans Petter Selasky return (USB_ERR_NOMEM); 3074d46dc4adSHans Petter Selasky } 3075a5cf1aaaSHans Petter Selasky 3076a5cf1aaaSHans Petter Selasky /* 3077a5cf1aaaSHans Petter Selasky * The following function is used to select the endpoint mode. It 3078a5cf1aaaSHans Petter Selasky * should not be called outside enumeration context. 3079a5cf1aaaSHans Petter Selasky */ 3080a5cf1aaaSHans Petter Selasky 3081a5cf1aaaSHans Petter Selasky usb_error_t 3082a5cf1aaaSHans Petter Selasky usbd_set_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep, 3083a5cf1aaaSHans Petter Selasky uint8_t ep_mode) 3084a5cf1aaaSHans Petter Selasky { 3085a5cf1aaaSHans Petter Selasky usb_error_t error; 3086f61fc4bdSHans Petter Selasky uint8_t do_unlock; 3087a5cf1aaaSHans Petter Selasky 30886950c75fSHans Petter Selasky /* Prevent re-enumeration */ 30896950c75fSHans Petter Selasky do_unlock = usbd_enum_lock(udev); 3090a5cf1aaaSHans Petter Selasky 3091a5cf1aaaSHans Petter Selasky if (udev->bus->methods->set_endpoint_mode != NULL) { 3092a5cf1aaaSHans Petter Selasky error = (udev->bus->methods->set_endpoint_mode) ( 3093a5cf1aaaSHans Petter Selasky udev, ep, ep_mode); 3094a5cf1aaaSHans Petter Selasky } else if (ep_mode != USB_EP_MODE_DEFAULT) { 3095a5cf1aaaSHans Petter Selasky error = USB_ERR_INVAL; 3096a5cf1aaaSHans Petter Selasky } else { 3097a5cf1aaaSHans Petter Selasky error = 0; 3098a5cf1aaaSHans Petter Selasky } 3099a5cf1aaaSHans Petter Selasky 3100a5cf1aaaSHans Petter Selasky /* only set new mode regardless of error */ 3101a5cf1aaaSHans Petter Selasky ep->ep_mode = ep_mode; 3102a5cf1aaaSHans Petter Selasky 3103f61fc4bdSHans Petter Selasky if (do_unlock) 3104f61fc4bdSHans Petter Selasky usbd_enum_unlock(udev); 3105a5cf1aaaSHans Petter Selasky return (error); 3106a5cf1aaaSHans Petter Selasky } 3107a5cf1aaaSHans Petter Selasky 3108a5cf1aaaSHans Petter Selasky uint8_t 3109a5cf1aaaSHans Petter Selasky usbd_get_endpoint_mode(struct usb_device *udev, struct usb_endpoint *ep) 3110a5cf1aaaSHans Petter Selasky { 3111a5cf1aaaSHans Petter Selasky return (ep->ep_mode); 3112a5cf1aaaSHans Petter Selasky } 3113