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