xref: /haiku/src/libs/compat/freebsd_network/fbsd_usb_lookup.c (revision 52f7c9389475e19fc21487b38064b4390eeb6fea)
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 #ifdef USB_GLOBAL_INCLUDE_FILE
28 #include USB_GLOBAL_INCLUDE_FILE
29 #else
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/module.h>
37 #include <sys/lock.h>
38 #include <sys/mutex.h>
39 #include <sys/condvar.h>
40 #include <sys/sysctl.h>
41 #include <sys/callout.h>
42 #include <sys/malloc.h>
43 #include <sys/priv.h>
44 #include <sys/limits.h>
45 #include <sys/endian.h>
46 
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #endif			/* USB_GLOBAL_INCLUDE_FILE */
50 
51 /*------------------------------------------------------------------------*
52  *	usbd_lookup_id_by_info
53  *
54  * This functions takes an array of "struct usb_device_id" and tries
55  * to match the entries with the information in "struct usbd_lookup_info".
56  *
57  * NOTE: The "sizeof_id" parameter must be a multiple of the
58  * usb_device_id structure size. Else the behaviour of this function
59  * is undefined.
60  *
61  * Return values:
62  * NULL: No match found.
63  * Else: Pointer to matching entry.
64  *------------------------------------------------------------------------*/
65 const struct usb_device_id *
66 usbd_lookup_id_by_info(const struct usb_device_id *id, usb_size_t sizeof_id,
67     const struct usbd_lookup_info *info)
68 {
69 	const struct usb_device_id *id_end;
70 
71 	if (id == NULL) {
72 		goto done;
73 	}
74 	id_end = (const void *)(((const uint8_t *)id) + sizeof_id);
75 
76 	/*
77 	 * Keep on matching array entries until we find a match or
78 	 * until we reach the end of the matching array:
79 	 */
80 	for (; id != id_end; id++) {
81 
82 		if ((id->match_flag_vendor) &&
83 		    (id->idVendor != info->idVendor)) {
84 			continue;
85 		}
86 		if ((id->match_flag_product) &&
87 		    (id->idProduct != info->idProduct)) {
88 			continue;
89 		}
90 		if ((id->match_flag_dev_lo) &&
91 		    (id->bcdDevice_lo > info->bcdDevice)) {
92 			continue;
93 		}
94 		if ((id->match_flag_dev_hi) &&
95 		    (id->bcdDevice_hi < info->bcdDevice)) {
96 			continue;
97 		}
98 		if ((id->match_flag_dev_class) &&
99 		    (id->bDeviceClass != info->bDeviceClass)) {
100 			continue;
101 		}
102 		if ((id->match_flag_dev_subclass) &&
103 		    (id->bDeviceSubClass != info->bDeviceSubClass)) {
104 			continue;
105 		}
106 		if ((id->match_flag_dev_protocol) &&
107 		    (id->bDeviceProtocol != info->bDeviceProtocol)) {
108 			continue;
109 		}
110 		if ((id->match_flag_int_class) &&
111 		    (id->bInterfaceClass != info->bInterfaceClass)) {
112 			continue;
113 		}
114 		if ((id->match_flag_int_subclass) &&
115 		    (id->bInterfaceSubClass != info->bInterfaceSubClass)) {
116 			continue;
117 		}
118 		if ((id->match_flag_int_protocol) &&
119 		    (id->bInterfaceProtocol != info->bInterfaceProtocol)) {
120 			continue;
121 		}
122 		/* We found a match! */
123 		return (id);
124 	}
125 
126 done:
127 	return (NULL);
128 }
129 
130 /*------------------------------------------------------------------------*
131  *	usbd_lookup_id_by_uaa - factored out code
132  *
133  * Return values:
134  *    0: Success
135  * Else: Failure
136  *------------------------------------------------------------------------*/
137 int
138 usbd_lookup_id_by_uaa(const struct usb_device_id *id, usb_size_t sizeof_id,
139     struct usb_attach_arg *uaa)
140 {
141 	id = usbd_lookup_id_by_info(id, sizeof_id, &uaa->info);
142 	if (id) {
143 		/* copy driver info */
144 		uaa->driver_info = id->driver_info;
145 		return (0);
146 	}
147 	return (ENXIO);
148 }
149 
150 /*------------------------------------------------------------------------*
151  *	Export the USB device ID format we use to userspace tools.
152  *------------------------------------------------------------------------*/
153 #if BYTE_ORDER == LITTLE_ENDIAN
154 #define	U16_XOR "0"
155 #else
156 #define	U16_XOR "8"
157 #endif
158 
159 #if defined(KLD_MODULE) && (USB_HAVE_ID_SECTION != 0)
160 static const char __section("bus_autoconf_format") __used usb_id_format[] = {
161 
162 	/* Declare that three different sections use the same format */
163 
164 	"usb_host_id{256,:}"
165 	"usb_device_id{256,:}"
166 	"usb_dual_id{256,:}"
167 
168 	/* List size of fields in the usb_device_id structure */
169 
170 	"mf_vendor{" U16_XOR ",1}"
171 	"mf_product{" U16_XOR ",1}"
172 	"mf_dev_lo{" U16_XOR ",1}"
173 	"mf_dev_hi{" U16_XOR ",1}"
174 
175 	"mf_dev_class{" U16_XOR ",1}"
176 	"mf_dev_subclass{" U16_XOR ",1}"
177 	"mf_dev_protocol{" U16_XOR ",1}"
178 	"mf_int_class{" U16_XOR ",1}"
179 
180 	"mf_int_subclass{" U16_XOR ",1}"
181 	"mf_int_protocol{" U16_XOR ",1}"
182 	"unused{" U16_XOR ",6}"
183 
184 	"idVendor[0]{" U16_XOR ",8}"
185 	"idVendor[1]{" U16_XOR ",8}"
186 	"idProduct[0]{" U16_XOR ",8}"
187 	"idProduct[1]{" U16_XOR ",8}"
188 	"bcdDevice_lo[0]{" U16_XOR ",8}"
189 	"bcdDevice_lo[1]{" U16_XOR ",8}"
190 	"bcdDevice_hi[0]{" U16_XOR ",8}"
191 	"bcdDevice_hi[1]{" U16_XOR ",8}"
192 
193 	"bDeviceClass{0,8}"
194 	"bDeviceSubClass{0,8}"
195 	"bDeviceProtocol{0,8}"
196 	"bInterfaceClass{0,8}"
197 	"bInterfaceSubClass{0,8}"
198 	"bInterfaceProtocol{0,8}"
199 
200 #if USB_HAVE_COMPAT_LINUX
201 	"mfl_vendor{" U16_XOR ",1}"
202 	"mfl_product{" U16_XOR ",1}"
203 	"mfl_dev_lo{" U16_XOR ",1}"
204 	"mfl_dev_hi{" U16_XOR ",1}"
205 
206 	"mfl_dev_class{" U16_XOR ",1}"
207 	"mfl_dev_subclass{" U16_XOR ",1}"
208 	"mfl_dev_protocol{" U16_XOR ",1}"
209 	"mfl_int_class{" U16_XOR ",1}"
210 
211 	"mfl_int_subclass{" U16_XOR ",1}"
212 	"mfl_int_protocol{" U16_XOR ",1}"
213 	"unused{" U16_XOR ",6}"
214 #endif
215 };
216 #endif
217