102ac6454SAndrew Thompson /* $FreeBSD$ */ 202ac6454SAndrew Thompson /*- 302ac6454SAndrew Thompson * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 402ac6454SAndrew Thompson * 502ac6454SAndrew Thompson * Redistribution and use in source and binary forms, with or without 602ac6454SAndrew Thompson * modification, are permitted provided that the following conditions 702ac6454SAndrew Thompson * are met: 802ac6454SAndrew Thompson * 1. Redistributions of source code must retain the above copyright 902ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer. 1002ac6454SAndrew Thompson * 2. Redistributions in binary form must reproduce the above copyright 1102ac6454SAndrew Thompson * notice, this list of conditions and the following disclaimer in the 1202ac6454SAndrew Thompson * documentation and/or other materials provided with the distribution. 1302ac6454SAndrew Thompson * 1402ac6454SAndrew Thompson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1502ac6454SAndrew Thompson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1602ac6454SAndrew Thompson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1702ac6454SAndrew Thompson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1802ac6454SAndrew Thompson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1902ac6454SAndrew Thompson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2002ac6454SAndrew Thompson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2102ac6454SAndrew Thompson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2202ac6454SAndrew Thompson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2302ac6454SAndrew Thompson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2402ac6454SAndrew Thompson * SUCH DAMAGE. 2502ac6454SAndrew Thompson */ 2602ac6454SAndrew Thompson 27d2b99310SHans Petter Selasky #ifdef USB_GLOBAL_INCLUDE_FILE 28d2b99310SHans Petter Selasky #include USB_GLOBAL_INCLUDE_FILE 29d2b99310SHans Petter Selasky #else 30ed6d949aSAndrew Thompson #include <sys/stdint.h> 31ed6d949aSAndrew Thompson #include <sys/stddef.h> 32ed6d949aSAndrew Thompson #include <sys/param.h> 33ed6d949aSAndrew Thompson #include <sys/queue.h> 34ed6d949aSAndrew Thompson #include <sys/types.h> 35ed6d949aSAndrew Thompson #include <sys/systm.h> 36ed6d949aSAndrew Thompson #include <sys/kernel.h> 37ed6d949aSAndrew Thompson #include <sys/bus.h> 38ed6d949aSAndrew Thompson #include <sys/module.h> 39ed6d949aSAndrew Thompson #include <sys/lock.h> 40ed6d949aSAndrew Thompson #include <sys/mutex.h> 41ed6d949aSAndrew Thompson #include <sys/condvar.h> 42ed6d949aSAndrew Thompson #include <sys/sysctl.h> 43ed6d949aSAndrew Thompson #include <sys/sx.h> 44ed6d949aSAndrew Thompson #include <sys/unistd.h> 45ed6d949aSAndrew Thompson #include <sys/callout.h> 46ed6d949aSAndrew Thompson #include <sys/malloc.h> 47ed6d949aSAndrew Thompson #include <sys/priv.h> 4802ac6454SAndrew Thompson 49ed6d949aSAndrew Thompson #include <dev/usb/usb.h> 50ed6d949aSAndrew Thompson #include <dev/usb/usbdi.h> 51ed6d949aSAndrew Thompson #include <dev/usb/usbdi_util.h> 52*5b0752bbSHans Petter Selasky 53*5b0752bbSHans Petter Selasky #define USB_DEBUG_VAR usb_debug 54*5b0752bbSHans Petter Selasky 55*5b0752bbSHans Petter Selasky #include <dev/usb/usb_core.h> 56*5b0752bbSHans Petter Selasky #include <dev/usb/usb_debug.h> 57d2b99310SHans Petter Selasky #endif /* USB_GLOBAL_INCLUDE_FILE */ 5802ac6454SAndrew Thompson 5902ac6454SAndrew Thompson /*------------------------------------------------------------------------* 60a593f6b8SAndrew Thompson * usb_desc_foreach 6102ac6454SAndrew Thompson * 6202ac6454SAndrew Thompson * This function is the safe way to iterate across the USB config 6302ac6454SAndrew Thompson * descriptor. It contains several checks against invalid 6402ac6454SAndrew Thompson * descriptors. If the "desc" argument passed to this function is 6502ac6454SAndrew Thompson * "NULL" the first descriptor, if any, will be returned. 6602ac6454SAndrew Thompson * 6702ac6454SAndrew Thompson * Return values: 6802ac6454SAndrew Thompson * NULL: End of descriptors 6902ac6454SAndrew Thompson * Else: Next descriptor after "desc" 7002ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 71760bc48eSAndrew Thompson struct usb_descriptor * 72a593f6b8SAndrew Thompson usb_desc_foreach(struct usb_config_descriptor *cd, 73760bc48eSAndrew Thompson struct usb_descriptor *_desc) 7402ac6454SAndrew Thompson { 7502ac6454SAndrew Thompson uint8_t *desc_next; 7602ac6454SAndrew Thompson uint8_t *start; 7702ac6454SAndrew Thompson uint8_t *end; 7802ac6454SAndrew Thompson uint8_t *desc; 7902ac6454SAndrew Thompson 8002ac6454SAndrew Thompson /* be NULL safe */ 8102ac6454SAndrew Thompson if (cd == NULL) 8202ac6454SAndrew Thompson return (NULL); 8302ac6454SAndrew Thompson 8402ac6454SAndrew Thompson /* We assume that the "wTotalLength" has been checked. */ 8502ac6454SAndrew Thompson start = (uint8_t *)cd; 8602ac6454SAndrew Thompson end = start + UGETW(cd->wTotalLength); 8702ac6454SAndrew Thompson desc = (uint8_t *)_desc; 8802ac6454SAndrew Thompson 8902ac6454SAndrew Thompson /* Get start of next USB descriptor. */ 9002ac6454SAndrew Thompson if (desc == NULL) 9102ac6454SAndrew Thompson desc = start; 9202ac6454SAndrew Thompson else 9302ac6454SAndrew Thompson desc = desc + desc[0]; 9402ac6454SAndrew Thompson 9502ac6454SAndrew Thompson /* Check that the next USB descriptor is within the range. */ 9602ac6454SAndrew Thompson if ((desc < start) || (desc >= end)) 9702ac6454SAndrew Thompson return (NULL); /* out of range, or EOD */ 9802ac6454SAndrew Thompson 9902ac6454SAndrew Thompson /* Check that the second next USB descriptor is within range. */ 10002ac6454SAndrew Thompson desc_next = desc + desc[0]; 10102ac6454SAndrew Thompson if ((desc_next < start) || (desc_next > end)) 10202ac6454SAndrew Thompson return (NULL); /* out of range */ 10302ac6454SAndrew Thompson 10402ac6454SAndrew Thompson /* Check minimum descriptor length. */ 10502ac6454SAndrew Thompson if (desc[0] < 3) 10602ac6454SAndrew Thompson return (NULL); /* too short descriptor */ 10702ac6454SAndrew Thompson 10802ac6454SAndrew Thompson /* Return start of next descriptor. */ 109760bc48eSAndrew Thompson return ((struct usb_descriptor *)desc); 11002ac6454SAndrew Thompson } 11102ac6454SAndrew Thompson 11202ac6454SAndrew Thompson /*------------------------------------------------------------------------* 113a593f6b8SAndrew Thompson * usb_idesc_foreach 11402ac6454SAndrew Thompson * 115bdd41206SAndrew Thompson * This function will iterate the interface descriptors in the config 116bdd41206SAndrew Thompson * descriptor. The parse state structure should be zeroed before 117bdd41206SAndrew Thompson * calling this function the first time. 11802ac6454SAndrew Thompson * 11902ac6454SAndrew Thompson * Return values: 12002ac6454SAndrew Thompson * NULL: End of descriptors 12102ac6454SAndrew Thompson * Else: A valid interface descriptor 12202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 123760bc48eSAndrew Thompson struct usb_interface_descriptor * 124a593f6b8SAndrew Thompson usb_idesc_foreach(struct usb_config_descriptor *cd, 125760bc48eSAndrew Thompson struct usb_idesc_parse_state *ps) 12602ac6454SAndrew Thompson { 127760bc48eSAndrew Thompson struct usb_interface_descriptor *id; 128bdd41206SAndrew Thompson uint8_t new_iface; 12902ac6454SAndrew Thompson 130bdd41206SAndrew Thompson /* retrieve current descriptor */ 131760bc48eSAndrew Thompson id = (struct usb_interface_descriptor *)ps->desc; 132bdd41206SAndrew Thompson /* default is to start a new interface */ 133bdd41206SAndrew Thompson new_iface = 1; 13402ac6454SAndrew Thompson 135bdd41206SAndrew Thompson while (1) { 136760bc48eSAndrew Thompson id = (struct usb_interface_descriptor *) 137a593f6b8SAndrew Thompson usb_desc_foreach(cd, (struct usb_descriptor *)id); 138bdd41206SAndrew Thompson if (id == NULL) 139bdd41206SAndrew Thompson break; 140bdd41206SAndrew Thompson if ((id->bDescriptorType == UDESC_INTERFACE) && 141bdd41206SAndrew Thompson (id->bLength >= sizeof(*id))) { 142bdd41206SAndrew Thompson if (ps->iface_no_last == id->bInterfaceNumber) 143bdd41206SAndrew Thompson new_iface = 0; 144bdd41206SAndrew Thompson ps->iface_no_last = id->bInterfaceNumber; 145bdd41206SAndrew Thompson break; 146bdd41206SAndrew Thompson } 147bdd41206SAndrew Thompson } 14802ac6454SAndrew Thompson 149bdd41206SAndrew Thompson if (ps->desc == NULL) { 150*5b0752bbSHans Petter Selasky /* first time or zero descriptors */ 151bdd41206SAndrew Thompson } else if (new_iface) { 152bdd41206SAndrew Thompson /* new interface */ 153bdd41206SAndrew Thompson ps->iface_index ++; 154bdd41206SAndrew Thompson ps->iface_index_alt = 0; 15502ac6454SAndrew Thompson } else { 156bdd41206SAndrew Thompson /* new alternate interface */ 157bdd41206SAndrew Thompson ps->iface_index_alt ++; 15802ac6454SAndrew Thompson } 159*5b0752bbSHans Petter Selasky #if (USB_IFACE_MAX <= 0) 160*5b0752bbSHans Petter Selasky #error "USB_IFACE_MAX must be defined greater than zero" 161*5b0752bbSHans Petter Selasky #endif 162*5b0752bbSHans Petter Selasky /* check for too many interfaces */ 163*5b0752bbSHans Petter Selasky if (ps->iface_index >= USB_IFACE_MAX) { 164*5b0752bbSHans Petter Selasky DPRINTF("Interface limit reached\n"); 165*5b0752bbSHans Petter Selasky id = NULL; 166*5b0752bbSHans Petter Selasky } 16702ac6454SAndrew Thompson 168bdd41206SAndrew Thompson /* store and return current descriptor */ 169760bc48eSAndrew Thompson ps->desc = (struct usb_descriptor *)id; 17002ac6454SAndrew Thompson return (id); 17102ac6454SAndrew Thompson } 17202ac6454SAndrew Thompson 17302ac6454SAndrew Thompson /*------------------------------------------------------------------------* 174a593f6b8SAndrew Thompson * usb_edesc_foreach 17502ac6454SAndrew Thompson * 176bdd41206SAndrew Thompson * This function will iterate all the endpoint descriptors within an 177bdd41206SAndrew Thompson * interface descriptor. Starting value for the "ped" argument should 178bdd41206SAndrew Thompson * be a valid interface descriptor. 17902ac6454SAndrew Thompson * 18002ac6454SAndrew Thompson * Return values: 18102ac6454SAndrew Thompson * NULL: End of descriptors 18202ac6454SAndrew Thompson * Else: A valid endpoint descriptor 18302ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 184760bc48eSAndrew Thompson struct usb_endpoint_descriptor * 185a593f6b8SAndrew Thompson usb_edesc_foreach(struct usb_config_descriptor *cd, 186760bc48eSAndrew Thompson struct usb_endpoint_descriptor *ped) 18702ac6454SAndrew Thompson { 188760bc48eSAndrew Thompson struct usb_descriptor *desc; 18902ac6454SAndrew Thompson 190760bc48eSAndrew Thompson desc = ((struct usb_descriptor *)ped); 19102ac6454SAndrew Thompson 192a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 19302ac6454SAndrew Thompson if (desc->bDescriptorType == UDESC_INTERFACE) { 19402ac6454SAndrew Thompson break; 19502ac6454SAndrew Thompson } 19602ac6454SAndrew Thompson if (desc->bDescriptorType == UDESC_ENDPOINT) { 197bdd41206SAndrew Thompson if (desc->bLength < sizeof(*ped)) { 198963169b4SHans Petter Selasky /* endpoint descriptor is invalid */ 19902ac6454SAndrew Thompson break; 20002ac6454SAndrew Thompson } 201760bc48eSAndrew Thompson return ((struct usb_endpoint_descriptor *)desc); 20202ac6454SAndrew Thompson } 20302ac6454SAndrew Thompson } 20402ac6454SAndrew Thompson return (NULL); 20502ac6454SAndrew Thompson } 20602ac6454SAndrew Thompson 20702ac6454SAndrew Thompson /*------------------------------------------------------------------------* 208963169b4SHans Petter Selasky * usb_ed_comp_foreach 209963169b4SHans Petter Selasky * 210963169b4SHans Petter Selasky * This function will iterate all the endpoint companion descriptors 211963169b4SHans Petter Selasky * within an endpoint descriptor in an interface descriptor. Starting 212963169b4SHans Petter Selasky * value for the "ped" argument should be a valid endpoint companion 213963169b4SHans Petter Selasky * descriptor. 214963169b4SHans Petter Selasky * 215963169b4SHans Petter Selasky * Return values: 216963169b4SHans Petter Selasky * NULL: End of descriptors 217963169b4SHans Petter Selasky * Else: A valid endpoint companion descriptor 218963169b4SHans Petter Selasky *------------------------------------------------------------------------*/ 219963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor * 220963169b4SHans Petter Selasky usb_ed_comp_foreach(struct usb_config_descriptor *cd, 221963169b4SHans Petter Selasky struct usb_endpoint_ss_comp_descriptor *ped) 222963169b4SHans Petter Selasky { 223963169b4SHans Petter Selasky struct usb_descriptor *desc; 224963169b4SHans Petter Selasky 225963169b4SHans Petter Selasky desc = ((struct usb_descriptor *)ped); 226963169b4SHans Petter Selasky 227963169b4SHans Petter Selasky while ((desc = usb_desc_foreach(cd, desc))) { 228963169b4SHans Petter Selasky if (desc->bDescriptorType == UDESC_INTERFACE) 229963169b4SHans Petter Selasky break; 230963169b4SHans Petter Selasky if (desc->bDescriptorType == UDESC_ENDPOINT) 231963169b4SHans Petter Selasky break; 232963169b4SHans Petter Selasky if (desc->bDescriptorType == UDESC_ENDPOINT_SS_COMP) { 233963169b4SHans Petter Selasky if (desc->bLength < sizeof(*ped)) { 234963169b4SHans Petter Selasky /* endpoint companion descriptor is invalid */ 235963169b4SHans Petter Selasky break; 236963169b4SHans Petter Selasky } 237963169b4SHans Petter Selasky return ((struct usb_endpoint_ss_comp_descriptor *)desc); 238963169b4SHans Petter Selasky } 239963169b4SHans Petter Selasky } 240963169b4SHans Petter Selasky return (NULL); 241963169b4SHans Petter Selasky } 242963169b4SHans Petter Selasky 243963169b4SHans Petter Selasky /*------------------------------------------------------------------------* 244a593f6b8SAndrew Thompson * usbd_get_no_descriptors 24502ac6454SAndrew Thompson * 246bdd41206SAndrew Thompson * This function will count the total number of descriptors in the 247bdd41206SAndrew Thompson * configuration descriptor of type "type". 24802ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 249bdd41206SAndrew Thompson uint8_t 250a593f6b8SAndrew Thompson usbd_get_no_descriptors(struct usb_config_descriptor *cd, uint8_t type) 25102ac6454SAndrew Thompson { 252760bc48eSAndrew Thompson struct usb_descriptor *desc = NULL; 253bdd41206SAndrew Thompson uint8_t count = 0; 25402ac6454SAndrew Thompson 255a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 256bdd41206SAndrew Thompson if (desc->bDescriptorType == type) { 25702ac6454SAndrew Thompson count++; 258bdd41206SAndrew Thompson if (count == 0xFF) 259bdd41206SAndrew Thompson break; /* crazy */ 26002ac6454SAndrew Thompson } 26102ac6454SAndrew Thompson } 26202ac6454SAndrew Thompson return (count); 26302ac6454SAndrew Thompson } 26402ac6454SAndrew Thompson 26502ac6454SAndrew Thompson /*------------------------------------------------------------------------* 266a593f6b8SAndrew Thompson * usbd_get_no_alts 26702ac6454SAndrew Thompson * 26802ac6454SAndrew Thompson * Return value: 269bd73b187SAlfred Perlstein * Number of alternate settings for the given interface descriptor 270bd73b187SAlfred Perlstein * pointer. If the USB descriptor is corrupt, the returned value can 271bd73b187SAlfred Perlstein * be greater than the actual number of alternate settings. 27202ac6454SAndrew Thompson *------------------------------------------------------------------------*/ 273bdd41206SAndrew Thompson uint8_t 274a593f6b8SAndrew Thompson usbd_get_no_alts(struct usb_config_descriptor *cd, 275760bc48eSAndrew Thompson struct usb_interface_descriptor *id) 27602ac6454SAndrew Thompson { 277760bc48eSAndrew Thompson struct usb_descriptor *desc; 278bd73b187SAlfred Perlstein uint8_t n; 279bdd41206SAndrew Thompson uint8_t ifaceno; 280bdd41206SAndrew Thompson 281bd73b187SAlfred Perlstein /* Reset interface count */ 282bd73b187SAlfred Perlstein 283bd73b187SAlfred Perlstein n = 0; 284bd73b187SAlfred Perlstein 285bd73b187SAlfred Perlstein /* Get the interface number */ 286bd73b187SAlfred Perlstein 287bdd41206SAndrew Thompson ifaceno = id->bInterfaceNumber; 288bdd41206SAndrew Thompson 289bd73b187SAlfred Perlstein /* Iterate all the USB descriptors */ 29002ac6454SAndrew Thompson 291bd73b187SAlfred Perlstein desc = NULL; 292a593f6b8SAndrew Thompson while ((desc = usb_desc_foreach(cd, desc))) { 29302ac6454SAndrew Thompson if ((desc->bDescriptorType == UDESC_INTERFACE) && 29402ac6454SAndrew Thompson (desc->bLength >= sizeof(*id))) { 295760bc48eSAndrew Thompson id = (struct usb_interface_descriptor *)desc; 29602ac6454SAndrew Thompson if (id->bInterfaceNumber == ifaceno) { 29702ac6454SAndrew Thompson n++; 298bdd41206SAndrew Thompson if (n == 0xFF) 299bdd41206SAndrew Thompson break; /* crazy */ 300bd73b187SAlfred Perlstein } 30102ac6454SAndrew Thompson } 30202ac6454SAndrew Thompson } 30302ac6454SAndrew Thompson return (n); 30402ac6454SAndrew Thompson } 305