summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/dev/usb/usb_lookup.c
blob: b9ff59387c475e07b551af93a36d7488bf6dcf6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#include <machine/rtems-bsd-kernel-space.h>

/* $FreeBSD$ */
/*-
 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/stdint.h>
#include <sys/stddef.h>
#include <rtems/bsd/sys/param.h>
#include <sys/queue.h>
#include <rtems/bsd/sys/types.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/module.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/mutex.h>
#include <sys/condvar.h>
#include <sys/sysctl.h>
#include <sys/sx.h>
#include <rtems/bsd/sys/unistd.h>
#include <sys/callout.h>
#include <sys/malloc.h>
#include <sys/priv.h>
#include <sys/limits.h>
#include <sys/endian.h>

#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>

/*------------------------------------------------------------------------*
 *	usbd_lookup_id_by_info
 *
 * This functions takes an array of "struct usb_device_id" and tries
 * to match the entries with the information in "struct usbd_lookup_info".
 *
 * NOTE: The "sizeof_id" parameter must be a multiple of the
 * usb_device_id structure size. Else the behaviour of this function
 * is undefined.
 *
 * Return values:
 * NULL: No match found.
 * Else: Pointer to matching entry.
 *------------------------------------------------------------------------*/
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)
{
	const struct usb_device_id *id_end;

	if (id == NULL) {
		goto done;
	}
	id_end = (const void *)(((const uint8_t *)id) + sizeof_id);

	/*
	 * Keep on matching array entries until we find a match or
	 * until we reach the end of the matching array:
	 */
	for (; id != id_end; id++) {

		if ((id->match_flag_vendor) &&
		    (id->idVendor != info->idVendor)) {
			continue;
		}
		if ((id->match_flag_product) &&
		    (id->idProduct != info->idProduct)) {
			continue;
		}
		if ((id->match_flag_dev_lo) &&
		    (id->bcdDevice_lo > info->bcdDevice)) {
			continue;
		}
		if ((id->match_flag_dev_hi) &&
		    (id->bcdDevice_hi < info->bcdDevice)) {
			continue;
		}
		if ((id->match_flag_dev_class) &&
		    (id->bDeviceClass != info->bDeviceClass)) {
			continue;
		}
		if ((id->match_flag_dev_subclass) &&
		    (id->bDeviceSubClass != info->bDeviceSubClass)) {
			continue;
		}
		if ((id->match_flag_dev_protocol) &&
		    (id->bDeviceProtocol != info->bDeviceProtocol)) {
			continue;
		}
		if ((id->match_flag_int_class) &&
		    (id->bInterfaceClass != info->bInterfaceClass)) {
			continue;
		}
		if ((id->match_flag_int_subclass) &&
		    (id->bInterfaceSubClass != info->bInterfaceSubClass)) {
			continue;
		}
		if ((id->match_flag_int_protocol) &&
		    (id->bInterfaceProtocol != info->bInterfaceProtocol)) {
			continue;
		}
		/* We found a match! */
		return (id);
	}

done:
	return (NULL);
}

/*------------------------------------------------------------------------*
 *	usbd_lookup_id_by_uaa - factored out code
 *
 * Return values:
 *    0: Success
 * Else: Failure
 *------------------------------------------------------------------------*/
int
usbd_lookup_id_by_uaa(const struct usb_device_id *id, usb_size_t sizeof_id,
    struct usb_attach_arg *uaa)
{
	id = usbd_lookup_id_by_info(id, sizeof_id, &uaa->info);
	if (id) {
		/* copy driver info */
		uaa->driver_info = id->driver_info;
		return (0);
	}
	return (ENXIO);
}

/*------------------------------------------------------------------------*
 *	Export the USB device ID format we use to userspace tools.
 *------------------------------------------------------------------------*/
#if BYTE_ORDER == BIG_ENDIAN
#define	U16_XOR "8"
#define	U32_XOR "12"
#define	U64_XOR "56"
#define	U8_BITFIELD_XOR "7"
#define	U16_BITFIELD_XOR "15"
#define	U32_BITFIELD_XOR "31"
#define	U64_BITFIELD_XOR "63"
#else
#define	U16_XOR "0"
#define	U32_XOR "0"
#define	U64_XOR "0"
#define	U8_BITFIELD_XOR "0"
#define	U16_BITFIELD_XOR "0"
#define	U32_BITFIELD_XOR "0"
#define	U64_BITFIELD_XOR "0"
#endif

#if USB_HAVE_COMPAT_LINUX
#define	MFL_SIZE "1"
#else
#define	MFL_SIZE "0"
#endif

#ifdef KLD_MODULE
static const char __section("bus_autoconf_format") __used usb_id_format[] = {

	/* Declare that three different sections use the same format */

	"usb_host_id{256,:}"
	"usb_device_id{256,:}"
	"usb_dual_id{256,:}"

	/* List size of fields in the usb_device_id structure */

#if ULONG_MAX >= 0xFFFFFFFFUL
	"unused{0,8}"
	"unused{0,8}"
	"unused{0,8}"
	"unused{0,8}"
#if ULONG_MAX >= 0xFFFFFFFFFFFFFFFFULL
	"unused{0,8}"
	"unused{0,8}"
	"unused{0,8}"
	"unused{0,8}"
#endif
#else
#error "Please update code."
#endif

	"idVendor[0]{" U16_XOR ",8}"
	"idVendor[1]{" U16_XOR ",8}"
	"idProduct[0]{" U16_XOR ",8}"
	"idProduct[1]{" U16_XOR ",8}"
	"bcdDevice_lo[0]{" U16_XOR ",8}"
	"bcdDevice_lo[1]{" U16_XOR ",8}"
	"bcdDevice_hi[0]{" U16_XOR ",8}"
	"bcdDevice_hi[1]{" U16_XOR ",8}"

	"bDeviceClass{0,8}"
	"bDeviceSubClass{0,8}"
	"bDeviceProtocol{0,8}"
	"bInterfaceClass{0,8}"
	"bInterfaceSubClass{0,8}"
	"bInterfaceProtocol{0,8}"

	"mf_vendor{" U8_BITFIELD_XOR ",1}"
	"mf_product{" U8_BITFIELD_XOR ",1}"
	"mf_dev_lo{" U8_BITFIELD_XOR ",1}"
	"mf_dev_hi{" U8_BITFIELD_XOR ",1}"

	"mf_dev_class{" U8_BITFIELD_XOR ",1}"
	"mf_dev_subclass{" U8_BITFIELD_XOR ",1}"
	"mf_dev_protocol{" U8_BITFIELD_XOR ",1}"
	"mf_int_class{" U8_BITFIELD_XOR ",1}"

	"mf_int_subclass{" U8_BITFIELD_XOR ",1}"
	"mf_int_protocol{" U8_BITFIELD_XOR ",1}"
	"unused{" U8_BITFIELD_XOR ",6}"

	"mfl_vendor{" U16_XOR "," MFL_SIZE "}"
	"mfl_product{" U16_XOR "," MFL_SIZE "}"
	"mfl_dev_lo{" U16_XOR "," MFL_SIZE "}"
	"mfl_dev_hi{" U16_XOR "," MFL_SIZE "}"

	"mfl_dev_class{" U16_XOR "," MFL_SIZE "}"
	"mfl_dev_subclass{" U16_XOR "," MFL_SIZE "}"
	"mfl_dev_protocol{" U16_XOR "," MFL_SIZE "}"
	"mfl_int_class{" U16_XOR "," MFL_SIZE "}"

	"mfl_int_subclass{" U16_XOR "," MFL_SIZE "}"
	"mfl_int_protocol{" U16_XOR "," MFL_SIZE "}"
	"unused{" U16_XOR "," MFL_SIZE "}"
	"unused{" U16_XOR "," MFL_SIZE "}"

	"unused{" U16_XOR "," MFL_SIZE "}"
	"unused{" U16_XOR "," MFL_SIZE "}"
	"unused{" U16_XOR "," MFL_SIZE "}"
	"unused{" U16_XOR "," MFL_SIZE "}"
};
#endif