xref: /freebsd/sys/dev/usb/usb_lookup.c (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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 #ifdef USB_GLOBAL_INCLUDE_FILE
29 #include USB_GLOBAL_INCLUDE_FILE
30 #else
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49 #include <sys/limits.h>
50 #include <sys/endian.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 #endif			/* USB_GLOBAL_INCLUDE_FILE */
55 
56 /*------------------------------------------------------------------------*
57  *	usbd_lookup_id_by_info
58  *
59  * This functions takes an array of "struct usb_device_id" and tries
60  * to match the entries with the information in "struct usbd_lookup_info".
61  *
62  * NOTE: The "sizeof_id" parameter must be a multiple of the
63  * usb_device_id structure size. Else the behaviour of this function
64  * is undefined.
65  *
66  * Return values:
67  * NULL: No match found.
68  * Else: Pointer to matching entry.
69  *------------------------------------------------------------------------*/
70 const struct usb_device_id *
71 usbd_lookup_id_by_info(const struct usb_device_id *id, usb_size_t sizeof_id,
72     const struct usbd_lookup_info *info)
73 {
74 	const struct usb_device_id *id_end;
75 
76 	if (id == NULL) {
77 		goto done;
78 	}
79 	id_end = (const void *)(((const uint8_t *)id) + sizeof_id);
80 
81 	/*
82 	 * Keep on matching array entries until we find a match or
83 	 * until we reach the end of the matching array:
84 	 */
85 	for (; id != id_end; id++) {
86 		if ((id->match_flag_vendor) &&
87 		    (id->idVendor != info->idVendor)) {
88 			continue;
89 		}
90 		if ((id->match_flag_product) &&
91 		    (id->idProduct != info->idProduct)) {
92 			continue;
93 		}
94 		if ((id->match_flag_dev_lo) &&
95 		    (id->bcdDevice_lo > info->bcdDevice)) {
96 			continue;
97 		}
98 		if ((id->match_flag_dev_hi) &&
99 		    (id->bcdDevice_hi < info->bcdDevice)) {
100 			continue;
101 		}
102 		if ((id->match_flag_dev_class) &&
103 		    (id->bDeviceClass != info->bDeviceClass)) {
104 			continue;
105 		}
106 		if ((id->match_flag_dev_subclass) &&
107 		    (id->bDeviceSubClass != info->bDeviceSubClass)) {
108 			continue;
109 		}
110 		if ((id->match_flag_dev_protocol) &&
111 		    (id->bDeviceProtocol != info->bDeviceProtocol)) {
112 			continue;
113 		}
114 		if ((id->match_flag_int_class) &&
115 		    (id->bInterfaceClass != info->bInterfaceClass)) {
116 			continue;
117 		}
118 		if ((id->match_flag_int_subclass) &&
119 		    (id->bInterfaceSubClass != info->bInterfaceSubClass)) {
120 			continue;
121 		}
122 		if ((id->match_flag_int_protocol) &&
123 		    (id->bInterfaceProtocol != info->bInterfaceProtocol)) {
124 			continue;
125 		}
126 		/* We found a match! */
127 		return (id);
128 	}
129 
130 done:
131 	return (NULL);
132 }
133 
134 /*------------------------------------------------------------------------*
135  *	usbd_lookup_id_by_uaa - factored out code
136  *
137  * Return values:
138  *    0: Success
139  * Else: Failure
140  *------------------------------------------------------------------------*/
141 int
142 usbd_lookup_id_by_uaa(const struct usb_device_id *id, usb_size_t sizeof_id,
143     struct usb_attach_arg *uaa)
144 {
145 	id = usbd_lookup_id_by_info(id, sizeof_id, &uaa->info);
146 	if (id) {
147 		/* copy driver info */
148 		uaa->driver_info = id->driver_info;
149 		return (0);
150 	}
151 	return (ENXIO);
152 }
153