usb_lookup.c revision 190749
1/* $FreeBSD: head/sys/dev/usb/usb_lookup.c 190749 2009-04-05 21:24:15Z piso $ */
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#include <dev/usb/usb_core.h>
28#include <dev/usb/usb_lookup.h>
29
30/*------------------------------------------------------------------------*
31 *	usb2_lookup_id_by_info
32 *
33 * This functions takes an array of "struct usb2_device_id" and tries
34 * to match the entries with the information in "struct usb2_lookup_info".
35 *
36 * NOTE: The "sizeof_id" parameter must be a multiple of the
37 * usb2_device_id structure size. Else the behaviour of this function
38 * is undefined.
39 *
40 * Return values:
41 * NULL: No match found.
42 * Else: Pointer to matching entry.
43 *------------------------------------------------------------------------*/
44const struct usb2_device_id *
45usb2_lookup_id_by_info(const struct usb2_device_id *id, usb2_size_t sizeof_id,
46    const struct usb2_lookup_info *info)
47{
48	const struct usb2_device_id *id_end;
49
50	if (id == NULL) {
51		goto done;
52	}
53	id_end = (const void *)(((const uint8_t *)id) + sizeof_id);
54
55	/*
56	 * Keep on matching array entries until we find a match or
57	 * until we reach the end of the matching array:
58	 */
59	for (; id != id_end; id++) {
60
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 ((info->bDeviceClass == 0xFF) &&
90		    (!(id->match_flag_vendor)) &&
91		    ((id->match_flag_int_class) ||
92		    (id->match_flag_int_subclass) ||
93		    (id->match_flag_int_protocol))) {
94			continue;
95		}
96		if ((id->match_flag_int_class) &&
97		    (id->bInterfaceClass != info->bInterfaceClass)) {
98			continue;
99		}
100		if ((id->match_flag_int_subclass) &&
101		    (id->bInterfaceSubClass != info->bInterfaceSubClass)) {
102			continue;
103		}
104		if ((id->match_flag_int_protocol) &&
105		    (id->bInterfaceProtocol != info->bInterfaceProtocol)) {
106			continue;
107		}
108		/* We found a match! */
109		return (id);
110	}
111
112done:
113	return (NULL);
114}
115
116/*------------------------------------------------------------------------*
117 *	usb2_lookup_id_by_uaa - factored out code
118 *
119 * Return values:
120 *    0: Success
121 * Else: Failure
122 *------------------------------------------------------------------------*/
123int
124usb2_lookup_id_by_uaa(const struct usb2_device_id *id, usb2_size_t sizeof_id,
125    struct usb2_attach_arg *uaa)
126{
127	id = usb2_lookup_id_by_info(id, sizeof_id, &uaa->info);
128	if (id) {
129		/* copy driver info */
130		uaa->driver_info = id->driver_info;
131		return (0);
132	}
133	return (ENXIO);
134}
135