1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include "implementation/global_implementation.h" 29 30 /*------------------------------------------------------------------------* 31 * usbd_lookup_id_by_info 32 * 33 * This functions takes an array of "struct usb_device_id" and tries 34 * to match the entries with the information in "struct usbd_lookup_info". 35 * 36 * NOTE: The "sizeof_id" parameter must be a multiple of the 37 * usb_device_id structure size. Else the behaviour of this function 38 * is undefined. 39 * 40 * Return values: 41 * NULL: No match found. 42 * Else: Pointer to matching entry. 43 *------------------------------------------------------------------------*/ 44 const struct usb_device_id * 45 usbd_lookup_id_by_info(const struct usb_device_id *id, usb_size_t sizeof_id, 46 const struct usbd_lookup_info *info) 47 { 48 const struct usb_device_id *id_end; 49 50 if (id == NULL) { 51 goto done; 52 } 53 id_end = (const void *)(((const uint8_t *)id) + sizeof_id); 54 55 /* 56 * Keep on matching array entries until we find a match or 57 * until we reach the end of the matching array: 58 */ 59 for (; id != id_end; id++) { 60 if ((id->match_flag_vendor) && 61 (id->idVendor != info->idVendor)) { 62 continue; 63 } 64 if ((id->match_flag_product) && 65 (id->idProduct != info->idProduct)) { 66 continue; 67 } 68 if ((id->match_flag_dev_lo) && 69 (id->bcdDevice_lo > info->bcdDevice)) { 70 continue; 71 } 72 if ((id->match_flag_dev_hi) && 73 (id->bcdDevice_hi < info->bcdDevice)) { 74 continue; 75 } 76 if ((id->match_flag_dev_class) && 77 (id->bDeviceClass != info->bDeviceClass)) { 78 continue; 79 } 80 if ((id->match_flag_dev_subclass) && 81 (id->bDeviceSubClass != info->bDeviceSubClass)) { 82 continue; 83 } 84 if ((id->match_flag_dev_protocol) && 85 (id->bDeviceProtocol != info->bDeviceProtocol)) { 86 continue; 87 } 88 if ((id->match_flag_int_class) && 89 (id->bInterfaceClass != info->bInterfaceClass)) { 90 continue; 91 } 92 if ((id->match_flag_int_subclass) && 93 (id->bInterfaceSubClass != info->bInterfaceSubClass)) { 94 continue; 95 } 96 if ((id->match_flag_int_protocol) && 97 (id->bInterfaceProtocol != info->bInterfaceProtocol)) { 98 continue; 99 } 100 /* We found a match! */ 101 return (id); 102 } 103 104 done: 105 return (NULL); 106 } 107 108 /*------------------------------------------------------------------------* 109 * usbd_lookup_id_by_uaa - factored out code 110 * 111 * Return values: 112 * 0: Success 113 * Else: Failure 114 *------------------------------------------------------------------------*/ 115 int 116 usbd_lookup_id_by_uaa(const struct usb_device_id *id, usb_size_t sizeof_id, 117 struct usb_attach_arg *uaa) 118 { 119 id = usbd_lookup_id_by_info(id, sizeof_id, &uaa->info); 120 if (id) { 121 /* copy driver info */ 122 uaa->driver_info = id->driver_info; 123 return (0); 124 } 125 return (ENXIO); 126 } 127 128 #ifndef LITTLE_ENDIAN 129 #define LITTLE_ENDIAN 1234 130 #endif 131 132 #ifndef BYTE_ORDER 133 #define BYTE_ORDER LITTLE_ENDIAN 134 #endif 135 136 #ifndef BIG_ENDIAN 137 #define BIG_ENDIAN 4321 138 #endif 139 140 /*------------------------------------------------------------------------* 141 * Export the USB device ID format we use to userspace tools. 142 *------------------------------------------------------------------------*/ 143 #if BYTE_ORDER == BIG_ENDIAN 144 #define U16_XOR "8" 145 #define U32_XOR "12" 146 #define U64_XOR "56" 147 #define U8_BITFIELD_XOR "7" 148 #define U16_BITFIELD_XOR "15" 149 #define U32_BITFIELD_XOR "31" 150 #define U64_BITFIELD_XOR "63" 151 #else 152 #define U16_XOR "0" 153 #define U32_XOR "0" 154 #define U64_XOR "0" 155 #define U8_BITFIELD_XOR "0" 156 #define U16_BITFIELD_XOR "0" 157 #define U32_BITFIELD_XOR "0" 158 #define U64_BITFIELD_XOR "0" 159 #endif 160 161 #if USB_HAVE_COMPAT_LINUX 162 #define MFL_SIZE "1" 163 #else 164 #define MFL_SIZE "0" 165 #endif 166 167 #if defined(KLD_MODULE) && (USB_HAVE_ID_SECTION != 0) 168 static const char __section("bus_autoconf_format") __used usb_id_format[] = { 169 170 /* Declare that three different sections use the same format */ 171 172 "usb_host_id{256,:}" 173 "usb_device_id{256,:}" 174 "usb_dual_id{256,:}" 175 176 /* List size of fields in the usb_device_id structure */ 177 178 #if ULONG_MAX >= 0xFFFFFFFFUL 179 "unused{0,8}" 180 "unused{0,8}" 181 "unused{0,8}" 182 "unused{0,8}" 183 #if ULONG_MAX >= 0xFFFFFFFFFFFFFFFFULL 184 "unused{0,8}" 185 "unused{0,8}" 186 "unused{0,8}" 187 "unused{0,8}" 188 #endif 189 #else 190 #error "Please update code." 191 #endif 192 193 "idVendor[0]{" U16_XOR ",8}" 194 "idVendor[1]{" U16_XOR ",8}" 195 "idProduct[0]{" U16_XOR ",8}" 196 "idProduct[1]{" U16_XOR ",8}" 197 "bcdDevice_lo[0]{" U16_XOR ",8}" 198 "bcdDevice_lo[1]{" U16_XOR ",8}" 199 "bcdDevice_hi[0]{" U16_XOR ",8}" 200 "bcdDevice_hi[1]{" U16_XOR ",8}" 201 202 "bDeviceClass{0,8}" 203 "bDeviceSubClass{0,8}" 204 "bDeviceProtocol{0,8}" 205 "bInterfaceClass{0,8}" 206 "bInterfaceSubClass{0,8}" 207 "bInterfaceProtocol{0,8}" 208 209 "mf_vendor{" U8_BITFIELD_XOR ",1}" 210 "mf_product{" U8_BITFIELD_XOR ",1}" 211 "mf_dev_lo{" U8_BITFIELD_XOR ",1}" 212 "mf_dev_hi{" U8_BITFIELD_XOR ",1}" 213 214 "mf_dev_class{" U8_BITFIELD_XOR ",1}" 215 "mf_dev_subclass{" U8_BITFIELD_XOR ",1}" 216 "mf_dev_protocol{" U8_BITFIELD_XOR ",1}" 217 "mf_int_class{" U8_BITFIELD_XOR ",1}" 218 219 "mf_int_subclass{" U8_BITFIELD_XOR ",1}" 220 "mf_int_protocol{" U8_BITFIELD_XOR ",1}" 221 "unused{" U8_BITFIELD_XOR ",6}" 222 223 "mfl_vendor{" U16_XOR "," MFL_SIZE "}" 224 "mfl_product{" U16_XOR "," MFL_SIZE "}" 225 "mfl_dev_lo{" U16_XOR "," MFL_SIZE "}" 226 "mfl_dev_hi{" U16_XOR "," MFL_SIZE "}" 227 228 "mfl_dev_class{" U16_XOR "," MFL_SIZE "}" 229 "mfl_dev_subclass{" U16_XOR "," MFL_SIZE "}" 230 "mfl_dev_protocol{" U16_XOR "," MFL_SIZE "}" 231 "mfl_int_class{" U16_XOR "," MFL_SIZE "}" 232 233 "mfl_int_subclass{" U16_XOR "," MFL_SIZE "}" 234 "mfl_int_protocol{" U16_XOR "," MFL_SIZE "}" 235 "unused{" U16_XOR "," MFL_SIZE "}" 236 "unused{" U16_XOR "," MFL_SIZE "}" 237 238 "unused{" U16_XOR "," MFL_SIZE "}" 239 "unused{" U16_XOR "," MFL_SIZE "}" 240 "unused{" U16_XOR "," MFL_SIZE "}" 241 "unused{" U16_XOR "," MFL_SIZE "}" 242 }; 243 #endif 244