1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 #include <sys/stdint.h> 28 #include <sys/stddef.h> 29 #include <sys/param.h> 30 #include <sys/queue.h> 31 #include <sys/types.h> 32 #include <sys/systm.h> 33 #include <sys/kernel.h> 34 #include <sys/bus.h> 35 #include <sys/module.h> 36 #include <sys/lock.h> 37 #include <sys/mutex.h> 38 #include <sys/condvar.h> 39 #include <sys/sysctl.h> 40 #include <sys/sx.h> 41 #include <sys/unistd.h> 42 #include <sys/callout.h> 43 #include <sys/malloc.h> 44 #include <sys/priv.h> 45 46 #include <dev/usb/usb.h> 47 #include <dev/usb/usbdi.h> 48 49 /*------------------------------------------------------------------------* 50 * usbd_lookup_id_by_info 51 * 52 * This functions takes an array of "struct usb_device_id" and tries 53 * to match the entries with the information in "struct usbd_lookup_info". 54 * 55 * NOTE: The "sizeof_id" parameter must be a multiple of the 56 * usb_device_id structure size. Else the behaviour of this function 57 * is undefined. 58 * 59 * Return values: 60 * NULL: No match found. 61 * Else: Pointer to matching entry. 62 *------------------------------------------------------------------------*/ 63 const struct usb_device_id * 64 usbd_lookup_id_by_info(const struct usb_device_id *id, usb_size_t sizeof_id, 65 const struct usbd_lookup_info *info) 66 { 67 const struct usb_device_id *id_end; 68 69 if (id == NULL) { 70 goto done; 71 } 72 id_end = (const void *)(((const uint8_t *)id) + sizeof_id); 73 74 /* 75 * Keep on matching array entries until we find a match or 76 * until we reach the end of the matching array: 77 */ 78 for (; id != id_end; id++) { 79 80 if ((id->match_flag_vendor) && 81 (id->idVendor != info->idVendor)) { 82 continue; 83 } 84 if ((id->match_flag_product) && 85 (id->idProduct != info->idProduct)) { 86 continue; 87 } 88 if ((id->match_flag_dev_lo) && 89 (id->bcdDevice_lo > info->bcdDevice)) { 90 continue; 91 } 92 if ((id->match_flag_dev_hi) && 93 (id->bcdDevice_hi < info->bcdDevice)) { 94 continue; 95 } 96 if ((id->match_flag_dev_class) && 97 (id->bDeviceClass != info->bDeviceClass)) { 98 continue; 99 } 100 if ((id->match_flag_dev_subclass) && 101 (id->bDeviceSubClass != info->bDeviceSubClass)) { 102 continue; 103 } 104 if ((id->match_flag_dev_protocol) && 105 (id->bDeviceProtocol != info->bDeviceProtocol)) { 106 continue; 107 } 108 if ((info->bDeviceClass == 0xFF) && 109 (!(id->match_flag_vendor)) && 110 ((id->match_flag_int_class) || 111 (id->match_flag_int_subclass) || 112 (id->match_flag_int_protocol))) { 113 continue; 114 } 115 if ((id->match_flag_int_class) && 116 (id->bInterfaceClass != info->bInterfaceClass)) { 117 continue; 118 } 119 if ((id->match_flag_int_subclass) && 120 (id->bInterfaceSubClass != info->bInterfaceSubClass)) { 121 continue; 122 } 123 if ((id->match_flag_int_protocol) && 124 (id->bInterfaceProtocol != info->bInterfaceProtocol)) { 125 continue; 126 } 127 /* We found a match! */ 128 return (id); 129 } 130 131 done: 132 return (NULL); 133 } 134 135 /*------------------------------------------------------------------------* 136 * usbd_lookup_id_by_uaa - factored out code 137 * 138 * Return values: 139 * 0: Success 140 * Else: Failure 141 *------------------------------------------------------------------------*/ 142 int 143 usbd_lookup_id_by_uaa(const struct usb_device_id *id, usb_size_t sizeof_id, 144 struct usb_attach_arg *uaa) 145 { 146 id = usbd_lookup_id_by_info(id, sizeof_id, &uaa->info); 147 if (id) { 148 /* copy driver info */ 149 uaa->driver_info = id->driver_info; 150 return (0); 151 } 152 return (ENXIO); 153 } 154