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