• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $FreeBSD$ */
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 		if ((id->match_flag_vendor) &&
62 		    (id->idVendor != info->idVendor)) {
63 			continue;
64 		}
65 		if ((id->match_flag_product) &&
66 		    (id->idProduct != info->idProduct)) {
67 			continue;
68 		}
69 		if ((id->match_flag_dev_lo) &&
70 		    (id->bcdDevice_lo > info->bcdDevice)) {
71 			continue;
72 		}
73 		if ((id->match_flag_dev_hi) &&
74 		    (id->bcdDevice_hi < info->bcdDevice)) {
75 			continue;
76 		}
77 		if ((id->match_flag_dev_class) &&
78 		    (id->bDeviceClass != info->bDeviceClass)) {
79 			continue;
80 		}
81 		if ((id->match_flag_dev_subclass) &&
82 		    (id->bDeviceSubClass != info->bDeviceSubClass)) {
83 			continue;
84 		}
85 		if ((id->match_flag_dev_protocol) &&
86 		    (id->bDeviceProtocol != info->bDeviceProtocol)) {
87 			continue;
88 		}
89 		if ((id->match_flag_int_class) &&
90 		    (id->bInterfaceClass != info->bInterfaceClass)) {
91 			continue;
92 		}
93 		if ((id->match_flag_int_subclass) &&
94 		    (id->bInterfaceSubClass != info->bInterfaceSubClass)) {
95 			continue;
96 		}
97 		if ((id->match_flag_int_protocol) &&
98 		    (id->bInterfaceProtocol != info->bInterfaceProtocol)) {
99 			continue;
100 		}
101 		/* We found a match! */
102 		return (id);
103 	}
104 
105 done:
106 	return (NULL);
107 }
108 
109 /*------------------------------------------------------------------------*
110  *	usbd_lookup_id_by_uaa - factored out code
111  *
112  * Return values:
113  *    0: Success
114  * Else: Failure
115  *------------------------------------------------------------------------*/
116 int
usbd_lookup_id_by_uaa(const struct usb_device_id * id,usb_size_t sizeof_id,struct usb_attach_arg * uaa)117 usbd_lookup_id_by_uaa(const struct usb_device_id *id, usb_size_t sizeof_id,
118     struct usb_attach_arg *uaa)
119 {
120 	id = usbd_lookup_id_by_info(id, sizeof_id, &uaa->info);
121 	if (id) {
122 		/* copy driver info */
123 		uaa->driver_info = id->driver_info;
124 		return (0);
125 	}
126 	return (ENXIO);
127 }
128 
129 #ifndef LITTLE_ENDIAN
130 #define	LITTLE_ENDIAN 1234
131 #endif
132 
133 #ifndef BYTE_ORDER
134 #define	BYTE_ORDER LITTLE_ENDIAN
135 #endif
136 
137 #ifndef BIG_ENDIAN
138 #define	BIG_ENDIAN 4321
139 #endif
140 
141 /*------------------------------------------------------------------------*
142  *	Export the USB device ID format we use to userspace tools.
143  *------------------------------------------------------------------------*/
144 #if BYTE_ORDER == BIG_ENDIAN
145 #define	U16_XOR "8"
146 #define	U32_XOR "12"
147 #define	U64_XOR "56"
148 #define	U8_BITFIELD_XOR "7"
149 #define	U16_BITFIELD_XOR "15"
150 #define	U32_BITFIELD_XOR "31"
151 #define	U64_BITFIELD_XOR "63"
152 #else
153 #define	U16_XOR "0"
154 #define	U32_XOR "0"
155 #define	U64_XOR "0"
156 #define	U8_BITFIELD_XOR "0"
157 #define	U16_BITFIELD_XOR "0"
158 #define	U32_BITFIELD_XOR "0"
159 #define	U64_BITFIELD_XOR "0"
160 #endif
161 
162 #if USB_HAVE_COMPAT_LINUX
163 #define	MFL_SIZE "1"
164 #else
165 #define	MFL_SIZE "0"
166 #endif
167 
168 #if defined(KLD_MODULE) && (USB_HAVE_ID_SECTION != 0)
169 static const char __section("bus_autoconf_format") __used usb_id_format[] = {
170 
171 	/* Declare that three different sections use the same format */
172 
173 	"usb_host_id{256,:}"
174 	"usb_device_id{256,:}"
175 	"usb_dual_id{256,:}"
176 
177 	/* List size of fields in the usb_device_id structure */
178 
179 #if ULONG_MAX >= 0xFFFFFFFFUL
180 	"unused{0,8}"
181 	"unused{0,8}"
182 	"unused{0,8}"
183 	"unused{0,8}"
184 #if ULONG_MAX >= 0xFFFFFFFFFFFFFFFFULL
185 	"unused{0,8}"
186 	"unused{0,8}"
187 	"unused{0,8}"
188 	"unused{0,8}"
189 #endif
190 #else
191 #error "Please update code."
192 #endif
193 
194 	"idVendor[0]{" U16_XOR ",8}"
195 	"idVendor[1]{" U16_XOR ",8}"
196 	"idProduct[0]{" U16_XOR ",8}"
197 	"idProduct[1]{" U16_XOR ",8}"
198 	"bcdDevice_lo[0]{" U16_XOR ",8}"
199 	"bcdDevice_lo[1]{" U16_XOR ",8}"
200 	"bcdDevice_hi[0]{" U16_XOR ",8}"
201 	"bcdDevice_hi[1]{" U16_XOR ",8}"
202 
203 	"bDeviceClass{0,8}"
204 	"bDeviceSubClass{0,8}"
205 	"bDeviceProtocol{0,8}"
206 	"bInterfaceClass{0,8}"
207 	"bInterfaceSubClass{0,8}"
208 	"bInterfaceProtocol{0,8}"
209 
210 	"mf_vendor{" U8_BITFIELD_XOR ",1}"
211 	"mf_product{" U8_BITFIELD_XOR ",1}"
212 	"mf_dev_lo{" U8_BITFIELD_XOR ",1}"
213 	"mf_dev_hi{" U8_BITFIELD_XOR ",1}"
214 
215 	"mf_dev_class{" U8_BITFIELD_XOR ",1}"
216 	"mf_dev_subclass{" U8_BITFIELD_XOR ",1}"
217 	"mf_dev_protocol{" U8_BITFIELD_XOR ",1}"
218 	"mf_int_class{" U8_BITFIELD_XOR ",1}"
219 
220 	"mf_int_subclass{" U8_BITFIELD_XOR ",1}"
221 	"mf_int_protocol{" U8_BITFIELD_XOR ",1}"
222 	"unused{" U8_BITFIELD_XOR ",6}"
223 
224 	"mfl_vendor{" U16_XOR "," MFL_SIZE "}"
225 	"mfl_product{" U16_XOR "," MFL_SIZE "}"
226 	"mfl_dev_lo{" U16_XOR "," MFL_SIZE "}"
227 	"mfl_dev_hi{" U16_XOR "," MFL_SIZE "}"
228 
229 	"mfl_dev_class{" U16_XOR "," MFL_SIZE "}"
230 	"mfl_dev_subclass{" U16_XOR "," MFL_SIZE "}"
231 	"mfl_dev_protocol{" U16_XOR "," MFL_SIZE "}"
232 	"mfl_int_class{" U16_XOR "," MFL_SIZE "}"
233 
234 	"mfl_int_subclass{" U16_XOR "," MFL_SIZE "}"
235 	"mfl_int_protocol{" U16_XOR "," MFL_SIZE "}"
236 	"unused{" U16_XOR "," MFL_SIZE "}"
237 	"unused{" U16_XOR "," MFL_SIZE "}"
238 
239 	"unused{" U16_XOR "," MFL_SIZE "}"
240 	"unused{" U16_XOR "," MFL_SIZE "}"
241 	"unused{" U16_XOR "," MFL_SIZE "}"
242 	"unused{" U16_XOR "," MFL_SIZE "}"
243 };
244 #endif
245