xref: /freebsd/sys/dev/usb/quirk/usb_quirk.c (revision 868a59a7e296bed53dd7b986e79964eed8a59f71)
1 /* $FreeBSD$ */
2 /*-
3  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
4  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
5  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/stdint.h>
30 #include <sys/stddef.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/bus.h>
37 #include <sys/module.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
42 #include <sys/sx.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
46 #include <sys/priv.h>
47 
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usb_ioctl.h>
50 #include <dev/usb/usbdi.h>
51 #include "usbdevs.h"
52 
53 #define	USB_DEBUG_VAR usb_debug
54 #include <dev/usb/usb_debug.h>
55 #include <dev/usb/usb_dynamic.h>
56 
57 #include <dev/usb/quirk/usb_quirk.h>
58 
59 MODULE_DEPEND(usb_quirk, usb, 1, 1, 1);
60 MODULE_VERSION(usb_quirk, 1);
61 
62 #define	USB_DEV_QUIRKS_MAX 384
63 #define	USB_SUB_QUIRKS_MAX 8
64 #define	USB_QUIRK_ENVROOT "hw.usb.quirk."
65 
66 struct usb_quirk_entry {
67 	uint16_t vid;
68 	uint16_t pid;
69 	uint16_t lo_rev;
70 	uint16_t hi_rev;
71 	uint16_t quirks[USB_SUB_QUIRKS_MAX];
72 };
73 
74 static struct mtx usb_quirk_mtx;
75 
76 #define	USB_QUIRK_VP(v,p,l,h,...) \
77   { .vid = (v), .pid = (p), .lo_rev = (l), .hi_rev = (h), \
78     .quirks = { __VA_ARGS__ } }
79 #define	USB_QUIRK(v,p,l,h,...) \
80   USB_QUIRK_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p, l, h, __VA_ARGS__)
81 
82 static struct usb_quirk_entry usb_quirks[USB_DEV_QUIRKS_MAX] = {
83 	USB_QUIRK(ASUS, LCM, 0x0000, 0xffff, UQ_HID_IGNORE),
84 	USB_QUIRK(INSIDEOUT, EDGEPORT4, 0x094, 0x094, UQ_SWAP_UNICODE),
85 	USB_QUIRK(DALLAS, J6502, 0x0a2, 0x0a2, UQ_BAD_ADC),
86 	USB_QUIRK(DALLAS, J6502, 0x0a2, 0x0a2, UQ_AU_NO_XU),
87 	USB_QUIRK(ALTEC, ADA70, 0x103, 0x103, UQ_BAD_ADC),
88 	USB_QUIRK(ALTEC, ASC495, 0x000, 0x000, UQ_BAD_AUDIO),
89 	USB_QUIRK(QTRONIX, 980N, 0x110, 0x110, UQ_SPUR_BUT_UP),
90 	USB_QUIRK(ALCOR2, KBD_HUB, 0x001, 0x001, UQ_SPUR_BUT_UP),
91 	USB_QUIRK(MCT, HUB0100, 0x102, 0x102, UQ_BUS_POWERED),
92 	USB_QUIRK(MCT, USB232, 0x102, 0x102, UQ_BUS_POWERED),
93 	USB_QUIRK(TI, UTUSB41, 0x110, 0x110, UQ_POWER_CLAIM),
94 	USB_QUIRK(TELEX, MIC1, 0x009, 0x009, UQ_AU_NO_FRAC),
95 	USB_QUIRK(SILICONPORTALS, YAPPHONE, 0x100, 0x100, UQ_AU_INP_ASYNC),
96 	USB_QUIRK(LOGITECH, UN53B, 0x0000, 0xffff, UQ_NO_STRINGS),
97 	USB_QUIRK(REALTEK, RTL8196EU, 0x0000, 0xffff, UQ_CFG_INDEX_1),
98 	USB_QUIRK(REALTEK, RTL8153, 0x0000, 0xffff, UQ_CFG_INDEX_1),
99 	USB_QUIRK(ELSA, MODEM1, 0x0000, 0xffff, UQ_CFG_INDEX_1),
100 	USB_QUIRK(PLANEX2, MZKUE150N, 0x0000, 0xffff, UQ_CFG_INDEX_1),
101 	USB_QUIRK(CISCOLINKSYS, USB3GIGV1, 0x0000, 0xffff, UQ_CFG_INDEX_1),
102 	/* Quirks for printer devices */
103 	USB_QUIRK(HP, 895C, 0x0000, 0xffff, UQ_BROKEN_BIDIR),
104 	USB_QUIRK(HP, 880C, 0x0000, 0xffff, UQ_BROKEN_BIDIR),
105 	USB_QUIRK(HP, 815C, 0x0000, 0xffff, UQ_BROKEN_BIDIR),
106 	USB_QUIRK(HP, 810C, 0x0000, 0xffff, UQ_BROKEN_BIDIR),
107 	USB_QUIRK(HP, 830C, 0x0000, 0xffff, UQ_BROKEN_BIDIR),
108 	USB_QUIRK(HP, 1220C, 0x0000, 0xffff, UQ_BROKEN_BIDIR),
109 	USB_QUIRK(XEROX, WCM15, 0x0000, 0xffff, UQ_BROKEN_BIDIR),
110 	/* Devices which should be ignored by uhid */
111 	USB_QUIRK(APC, UPS, 0x0000, 0xffff, UQ_HID_IGNORE),
112 	USB_QUIRK(BELKIN, F6C550AVR, 0x0000, 0xffff, UQ_HID_IGNORE),
113 	USB_QUIRK(CYBERPOWER, 1500CAVRLCD, 0x0000, 0xffff, UQ_HID_IGNORE),
114 	USB_QUIRK(CYPRESS, SILVERSHIELD, 0x0000, 0xffff, UQ_HID_IGNORE),
115 	USB_QUIRK(DELORME, EARTHMATE, 0x0000, 0xffff, UQ_HID_IGNORE),
116 	USB_QUIRK(DREAMLINK, DL100B, 0x0000, 0xffff, UQ_HID_IGNORE),
117 	USB_QUIRK(ITUNERNET, USBLCD2X20, 0x0000, 0xffff, UQ_HID_IGNORE),
118 	USB_QUIRK(ITUNERNET, USBLCD4X20, 0x0000, 0xffff, UQ_HID_IGNORE),
119 	USB_QUIRK(LIEBERT, POWERSURE_PXT, 0x0000, 0xffff, UQ_HID_IGNORE),
120 	USB_QUIRK(LIEBERT2, PSI1000, 0x0000, 0xffff, UQ_HID_IGNORE),
121 	USB_QUIRK(MGE, UPS1, 0x0000, 0xffff, UQ_HID_IGNORE),
122 	USB_QUIRK(MGE, UPS2, 0x0000, 0xffff, UQ_HID_IGNORE),
123 	USB_QUIRK(APPLE, IPHONE, 0x0000, 0xffff, UQ_HID_IGNORE),
124 	USB_QUIRK(APPLE, IPHONE_3G, 0x0000, 0xffff, UQ_HID_IGNORE),
125 	USB_QUIRK(MEGATEC, UPS, 0x0000, 0xffff, UQ_HID_IGNORE),
126 	/* Devices which should be ignored by both ukbd and uhid */
127 	USB_QUIRK(CYPRESS, WISPY1A, 0x0000, 0xffff, UQ_KBD_IGNORE, UQ_HID_IGNORE),
128 	USB_QUIRK(METAGEEK, WISPY1B, 0x0000, 0xffff, UQ_KBD_IGNORE, UQ_HID_IGNORE),
129 	USB_QUIRK(METAGEEK, WISPY24X, 0x0000, 0xffff, UQ_KBD_IGNORE, UQ_HID_IGNORE),
130 	USB_QUIRK(METAGEEK2, WISPYDBX, 0x0000, 0xffff, UQ_KBD_IGNORE, UQ_HID_IGNORE),
131 	USB_QUIRK(TENX, UAUDIO0, 0x0101, 0x0101, UQ_AUDIO_SWAP_LR),
132 	/* MS keyboards do weird things */
133 	USB_QUIRK(MICROSOFT, NATURAL4000, 0x0000, 0xFFFF, UQ_KBD_BOOTPROTO),
134 	USB_QUIRK(MICROSOFT, WLINTELLIMOUSE, 0x0000, 0xffff, UQ_MS_LEADING_BYTE),
135 	/* Quirk for Corsair Vengeance K60 keyboard */
136 	USB_QUIRK(CORSAIR, K60, 0x0000, 0xffff, UQ_KBD_BOOTPROTO),
137 	/* Quirk for Corsair Vengeance K70 keyboard */
138 	USB_QUIRK(CORSAIR, K70, 0x0000, 0xffff, UQ_KBD_BOOTPROTO),
139 	/* umodem(4) device quirks */
140 	USB_QUIRK(METRICOM, RICOCHET_GS, 0x100, 0x100, UQ_ASSUME_CM_OVER_DATA),
141 	USB_QUIRK(SANYO, SCP4900, 0x000, 0x000, UQ_ASSUME_CM_OVER_DATA),
142 	USB_QUIRK(MOTOROLA2, T720C, 0x001, 0x001, UQ_ASSUME_CM_OVER_DATA),
143 	USB_QUIRK(EICON, DIVA852, 0x100, 0x100, UQ_ASSUME_CM_OVER_DATA),
144 	USB_QUIRK(SIEMENS2, ES75, 0x000, 0x000, UQ_ASSUME_CM_OVER_DATA),
145 	USB_QUIRK(QUALCOMM, CDMA_MSM, 0x0000, 0xffff, UQ_ASSUME_CM_OVER_DATA),
146 	USB_QUIRK(QUALCOMM2, CDMA_MSM, 0x0000, 0xffff, UQ_ASSUME_CM_OVER_DATA),
147 	USB_QUIRK(CURITEL, UM150, 0x0000, 0xffff, UQ_ASSUME_CM_OVER_DATA),
148 	USB_QUIRK(CURITEL, UM175, 0x0000, 0xffff, UQ_ASSUME_CM_OVER_DATA),
149 	USB_QUIRK(VERTEX, VW110L, 0x0000, 0xffff, UQ_ASSUME_CM_OVER_DATA),
150 
151 	/* USB Mass Storage Class Quirks */
152 	USB_QUIRK_VP(USB_VENDOR_ASAHIOPTICAL, 0, UQ_MSC_NO_RS_CLEAR_UA,
153 	    UQ_MATCH_VENDOR_ONLY),
154 	USB_QUIRK(ADDON, ATTACHE, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
155 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE),
156 	USB_QUIRK(ADDON, A256MB, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
157 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE),
158 	USB_QUIRK(ADDON, DISKPRO512, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
159 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE),
160 	USB_QUIRK(ADDONICS2, CABLE_205, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
161 	    UQ_MSC_FORCE_PROTO_SCSI),
162 	USB_QUIRK(AIPTEK, POCKETCAM3M, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
163 	    UQ_MSC_FORCE_PROTO_SCSI),
164 	USB_QUIRK(ALCOR, UMCR_9361, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
165 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_GETMAXLUN),
166 	USB_QUIRK(ALCOR, TRANSCEND, 0x0000, 0xffff, UQ_MSC_NO_GETMAXLUN,
167 	    UQ_MSC_NO_SYNC_CACHE, UQ_MSC_NO_TEST_UNIT_READY),
168 	USB_QUIRK(APACER, HT202, 0x0000, 0xffff, UQ_MSC_NO_TEST_UNIT_READY,
169 	    UQ_MSC_NO_SYNC_CACHE),
170 	USB_QUIRK(ASAHIOPTICAL, OPTIO230, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
171 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
172 	USB_QUIRK(ASAHIOPTICAL, OPTIO330, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
173 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
174 	USB_QUIRK(ATP, EUSB, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
175 	USB_QUIRK(BELKIN, USB2SCSI, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
176 	    UQ_MSC_FORCE_PROTO_SCSI),
177 	USB_QUIRK(CASIO, QV_DIGICAM, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
178 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
179 	USB_QUIRK(CCYU, ED1064, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
180 	    UQ_MSC_FORCE_PROTO_SCSI),
181 	USB_QUIRK(CENTURY, EX35QUAT, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
182 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_FORCE_SHORT_INQ,
183 	    UQ_MSC_NO_START_STOP, UQ_MSC_IGNORE_RESIDUE),
184 	USB_QUIRK(CYPRESS, XX6830XX, 0x0000, 0xffff, UQ_MSC_NO_GETMAXLUN,
185 	    UQ_MSC_NO_SYNC_CACHE),
186 	USB_QUIRK(DESKNOTE, UCR_61S2B, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
187 	    UQ_MSC_FORCE_PROTO_SCSI),
188 	USB_QUIRK(DMI, CFSM_RW, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI,
189 	    UQ_MSC_NO_GETMAXLUN),
190 	USB_QUIRK(EMTEC, RUF2PS, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
191 	USB_QUIRK(EPSON, STYLUS_875DC, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
192 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
193 	USB_QUIRK(EPSON, STYLUS_895, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
194 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_GETMAXLUN),
195 	USB_QUIRK(FEIYA, 5IN1, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
196 	    UQ_MSC_FORCE_PROTO_SCSI),
197 	USB_QUIRK(FEIYA, ELANGO, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
198 	USB_QUIRK(FREECOM, DVD, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
199 	USB_QUIRK(FUJIPHOTO, MASS0100, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI_I,
200 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_RS_CLEAR_UA, UQ_MSC_NO_SYNC_CACHE),
201 	USB_QUIRK(GENESYS, GL641USB2IDE, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
202 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_FORCE_SHORT_INQ,
203 	    UQ_MSC_NO_START_STOP, UQ_MSC_IGNORE_RESIDUE, UQ_MSC_NO_SYNC_CACHE),
204 	USB_QUIRK(GENESYS, GL641USB2IDE_2, 0x0000, 0xffff,
205 	    UQ_MSC_FORCE_WIRE_BBB, UQ_MSC_FORCE_PROTO_ATAPI,
206 	    UQ_MSC_FORCE_SHORT_INQ, UQ_MSC_NO_START_STOP,
207 	    UQ_MSC_IGNORE_RESIDUE),
208 	USB_QUIRK(GENESYS, GL641USB, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
209 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_FORCE_SHORT_INQ,
210 	    UQ_MSC_NO_START_STOP, UQ_MSC_IGNORE_RESIDUE),
211 	USB_QUIRK(GENESYS, GL641USB_2, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
212 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_WRONG_CSWSIG),
213 	USB_QUIRK(HAGIWARA, FG, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
214 	    UQ_MSC_FORCE_PROTO_SCSI),
215 	USB_QUIRK(HAGIWARA, FGSM, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
216 	    UQ_MSC_FORCE_PROTO_SCSI),
217 	USB_QUIRK(HITACHI, DVDCAM_DZ_MV100A, 0x0000, 0xffff,
218 	    UQ_MSC_FORCE_WIRE_CBI, UQ_MSC_FORCE_PROTO_SCSI,
219 	    UQ_MSC_NO_GETMAXLUN),
220 	USB_QUIRK(HITACHI, DVDCAM_USB, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI_I,
221 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_INQUIRY),
222 	USB_QUIRK(HP, CDW4E, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_ATAPI),
223 	USB_QUIRK(HP, CDW8200, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI_I,
224 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_TEST_UNIT_READY,
225 	    UQ_MSC_NO_START_STOP),
226 	USB_QUIRK(IMAGINATION, DBX1, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
227 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_WRONG_CSWSIG),
228 	USB_QUIRK(INSYSTEM, USBCABLE, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
229 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_TEST_UNIT_READY,
230 	    UQ_MSC_NO_START_STOP, UQ_MSC_ALT_IFACE_1),
231 	USB_QUIRK(INSYSTEM, ATAPI, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
232 	    UQ_MSC_FORCE_PROTO_RBC),
233 	USB_QUIRK(INSYSTEM, STORAGE_V2, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
234 	    UQ_MSC_FORCE_PROTO_RBC),
235 	USB_QUIRK(IODATA, IU_CD2, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
236 	    UQ_MSC_FORCE_PROTO_SCSI),
237 	USB_QUIRK(IODATA, DVR_UEH8, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
238 	    UQ_MSC_FORCE_PROTO_SCSI),
239 	USB_QUIRK(IOMEGA, ZIP100, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
240 	    UQ_MSC_FORCE_PROTO_SCSI,
241 	    UQ_MSC_NO_TEST_UNIT_READY), /* XXX ZIP drives can also use ATAPI */
242 	USB_QUIRK(JMICRON, JM20337, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
243 	    UQ_MSC_FORCE_PROTO_SCSI,
244 	    UQ_MSC_NO_SYNC_CACHE),
245 	USB_QUIRK(KINGSTON, HYPERX3_0, 0x0000, 0xffff, UQ_MSC_NO_INQUIRY),
246 	USB_QUIRK(KYOCERA, FINECAM_L3, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
247 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
248 	USB_QUIRK(KYOCERA, FINECAM_S3X, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
249 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_INQUIRY),
250 	USB_QUIRK(KYOCERA, FINECAM_S4, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
251 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_INQUIRY),
252 	USB_QUIRK(KYOCERA, FINECAM_S5, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
253 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
254 	USB_QUIRK(LACIE, HD, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
255 	    UQ_MSC_FORCE_PROTO_RBC),
256 	USB_QUIRK(LEXAR, CF_READER, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
257 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
258 	USB_QUIRK(LEXAR, JUMPSHOT, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
259 	USB_QUIRK(LEXAR, JUMPDRIVE, 0x0000, 0xffff, UQ_MSC_NO_INQUIRY),
260 	USB_QUIRK(LOGITEC, LDR_H443SU2, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
261 	USB_QUIRK(LOGITEC, LDR_H443U2, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
262 	    UQ_MSC_FORCE_PROTO_SCSI,),
263 	USB_QUIRK(MELCO, DUBPXXG, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
264 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_FORCE_SHORT_INQ,
265 	    UQ_MSC_NO_START_STOP, UQ_MSC_IGNORE_RESIDUE),
266 	USB_QUIRK(MICROTECH, DPCM, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
267 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_TEST_UNIT_READY,
268 	    UQ_MSC_NO_START_STOP),
269 	USB_QUIRK(MICRON, REALSSD, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
270 	USB_QUIRK(MICROTECH, SCSIDB25, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
271 	    UQ_MSC_FORCE_PROTO_SCSI),
272 	USB_QUIRK(MICROTECH, SCSIHD50, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
273 	    UQ_MSC_FORCE_PROTO_SCSI),
274 	USB_QUIRK(MINOLTA, E223, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
275 	USB_QUIRK(MINOLTA, F300, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
276 	    UQ_MSC_FORCE_PROTO_SCSI),
277 	USB_QUIRK(MITSUMI, CDRRW, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI |
278 	    UQ_MSC_FORCE_PROTO_ATAPI),
279 	USB_QUIRK(MOTOROLA2, E398, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
280 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_FORCE_SHORT_INQ,
281 	    UQ_MSC_NO_INQUIRY_EVPD, UQ_MSC_NO_GETMAXLUN),
282 	USB_QUIRK_VP(USB_VENDOR_MPMAN, 0, UQ_MSC_NO_SYNC_CACHE,
283 	    UQ_MATCH_VENDOR_ONLY),
284 	USB_QUIRK(MSYSTEMS, DISKONKEY, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
285 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE, UQ_MSC_NO_GETMAXLUN,
286 	    UQ_MSC_NO_RS_CLEAR_UA),
287 	USB_QUIRK(MSYSTEMS, DISKONKEY2, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
288 	    UQ_MSC_FORCE_PROTO_ATAPI),
289 	USB_QUIRK(MYSON, HEDEN, 0x0000, 0xffff, UQ_MSC_IGNORE_RESIDUE,
290 	    UQ_MSC_NO_SYNC_CACHE),
291 	USB_QUIRK(NEODIO, ND3260, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
292 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_FORCE_SHORT_INQ),
293 	USB_QUIRK(NETAC, CF_CARD, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
294 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
295 	USB_QUIRK(NETAC, ONLYDISK, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
296 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE),
297 	USB_QUIRK(NETCHIP, CLIK_40, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_ATAPI,
298 	    UQ_MSC_NO_INQUIRY),
299 	USB_QUIRK(NETCHIP, POCKETBOOK, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
300 	USB_QUIRK(NIKON, D300, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
301 	    UQ_MSC_FORCE_PROTO_SCSI),
302 	USB_QUIRK(OLYMPUS, C1, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
303 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_WRONG_CSWSIG),
304 	USB_QUIRK(OLYMPUS, C700, 0x0000, 0xffff, UQ_MSC_NO_GETMAXLUN),
305 	USB_QUIRK(ONSPEC, SDS_HOTFIND_D, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
306 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_GETMAXLUN, UQ_MSC_NO_SYNC_CACHE),
307 	USB_QUIRK(ONSPEC, CFMS_RW, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
308 	USB_QUIRK(ONSPEC, CFSM_COMBO, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
309 	USB_QUIRK(ONSPEC, CFSM_READER, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
310 	USB_QUIRK(ONSPEC, CFSM_READER2, 0x0000, 0xffff,
311 	    UQ_MSC_FORCE_PROTO_SCSI),
312 	USB_QUIRK(ONSPEC, MDCFE_B_CF_READER, 0x0000, 0xffff,
313 	    UQ_MSC_FORCE_PROTO_SCSI),
314 	USB_QUIRK(ONSPEC, MDSM_B_READER, 0x0000, 0xffff,
315 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
316 	USB_QUIRK(ONSPEC, READER, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
317 	USB_QUIRK(ONSPEC, UCF100, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
318 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_INQUIRY, UQ_MSC_NO_GETMAXLUN),
319 	USB_QUIRK(ONSPEC2, IMAGEMATE_SDDR55, 0x0000, 0xffff,
320 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_GETMAXLUN),
321 	USB_QUIRK(PANASONIC, KXL840AN, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
322 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_GETMAXLUN),
323 	USB_QUIRK(PANASONIC, KXLCB20AN, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
324 	    UQ_MSC_FORCE_PROTO_SCSI),
325 	USB_QUIRK(PANASONIC, KXLCB35AN, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
326 	    UQ_MSC_FORCE_PROTO_SCSI),
327 	USB_QUIRK(PANASONIC, LS120CAM, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_UFI),
328 	USB_QUIRK(PLEXTOR, 40_12_40U, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
329 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_TEST_UNIT_READY),
330 	USB_QUIRK(PNY, ATTACHE2, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
331 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE,
332 	    UQ_MSC_NO_START_STOP),
333 	USB_QUIRK(PROLIFIC, PL2506, 0x0000, 0xffff,
334 	    UQ_MSC_NO_SYNC_CACHE),
335 	USB_QUIRK_VP(USB_VENDOR_SAMSUNG_TECHWIN,
336 	    USB_PRODUCT_SAMSUNG_TECHWIN_DIGIMAX_410, UQ_MSC_FORCE_WIRE_BBB,
337 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
338 	USB_QUIRK(SANDISK, SDDR05A, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
339 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_READ_CAP_OFFBY1,
340 	    UQ_MSC_NO_GETMAXLUN),
341 	USB_QUIRK(SANDISK, SDDR09, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI,
342 	    UQ_MSC_READ_CAP_OFFBY1, UQ_MSC_NO_GETMAXLUN),
343 	USB_QUIRK(SANDISK, SDDR12, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
344 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_READ_CAP_OFFBY1,
345 	    UQ_MSC_NO_GETMAXLUN),
346 	USB_QUIRK(SANDISK, SDCZ2_128, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
347 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE,
348 	    UQ_MSC_NO_SYNC_CACHE),
349 	USB_QUIRK(SANDISK, SDCZ2_256, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
350 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE),
351 	USB_QUIRK(SANDISK, SDCZ4_128, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
352 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE),
353 	USB_QUIRK(SANDISK, SDCZ4_256, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
354 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE),
355 	USB_QUIRK(SANDISK, SDDR31, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
356 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_READ_CAP_OFFBY1),
357 	USB_QUIRK(SANDISK, IMAGEMATE_SDDR289, 0x0000, 0xffff,
358 	    UQ_MSC_NO_SYNC_CACHE, UQ_MSC_NO_GETMAXLUN),
359 	USB_QUIRK(SCANLOGIC, SL11R, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
360 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_INQUIRY),
361 	USB_QUIRK(SHUTTLE, EUSB, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI_I,
362 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_NO_TEST_UNIT_READY,
363 	    UQ_MSC_NO_START_STOP, UQ_MSC_SHUTTLE_INIT),
364 	USB_QUIRK(SHUTTLE, CDRW, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
365 	    UQ_MSC_FORCE_PROTO_ATAPI),
366 	USB_QUIRK(SHUTTLE, CF, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
367 	    UQ_MSC_FORCE_PROTO_ATAPI),
368 	USB_QUIRK(SHUTTLE, EUSBATAPI, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
369 	    UQ_MSC_FORCE_PROTO_ATAPI),
370 	USB_QUIRK(SHUTTLE, EUSBCFSM, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
371 	USB_QUIRK(SHUTTLE, EUSCSI, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
372 	    UQ_MSC_FORCE_PROTO_SCSI),
373 	USB_QUIRK(SHUTTLE, HIFD, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
374 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_GETMAXLUN),
375 	USB_QUIRK(SHUTTLE, SDDR09, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI,
376 	    UQ_MSC_NO_GETMAXLUN),
377 	USB_QUIRK(SHUTTLE, ZIOMMC, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
378 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_GETMAXLUN),
379 	USB_QUIRK(SIGMATEL, I_BEAD100, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
380 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_SHUTTLE_INIT),
381 	USB_QUIRK(SIIG, WINTERREADER, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
382 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_IGNORE_RESIDUE),
383 	USB_QUIRK(SKANHEX, MD_7425, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
384 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
385 	USB_QUIRK(SKANHEX, SX_520Z, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
386 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
387 	USB_QUIRK(SONY, HANDYCAM, 0x0500, 0x0500, UQ_MSC_FORCE_WIRE_CBI,
388 	    UQ_MSC_FORCE_PROTO_RBC, UQ_MSC_RBC_PAD_TO_12),
389 	USB_QUIRK(SONY, CLIE_40_MS, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
390 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
391 	USB_QUIRK(SONY, DSC, 0x0500, 0x0500, UQ_MSC_FORCE_WIRE_CBI,
392 	    UQ_MSC_FORCE_PROTO_RBC, UQ_MSC_RBC_PAD_TO_12),
393 	USB_QUIRK(SONY, DSC, 0x0600, 0x0600, UQ_MSC_FORCE_WIRE_CBI,
394 	    UQ_MSC_FORCE_PROTO_RBC, UQ_MSC_RBC_PAD_TO_12),
395 	USB_QUIRK(SONY, DSC, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
396 	    UQ_MSC_FORCE_PROTO_RBC),
397 	USB_QUIRK(SONY, HANDYCAM, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
398 	    UQ_MSC_FORCE_PROTO_RBC),
399 	USB_QUIRK(SONY, MSC, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
400 	    UQ_MSC_FORCE_PROTO_RBC),
401 	USB_QUIRK(SONY, MS_MSC_U03, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
402 	    UQ_MSC_FORCE_PROTO_UFI, UQ_MSC_NO_GETMAXLUN),
403 	USB_QUIRK(SONY, MS_NW_MS7, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
404 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_GETMAXLUN),
405 	USB_QUIRK(SONY, MS_PEG_N760C, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
406 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
407 	USB_QUIRK(SONY, MSACUS1, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
408 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_GETMAXLUN),
409 	USB_QUIRK(SONY, PORTABLE_HDD_V2, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
410 	    UQ_MSC_FORCE_PROTO_SCSI),
411 	USB_QUIRK(STMICRO, ST72682, 0x0000, 0xffff, UQ_MSC_NO_PREVENT_ALLOW),
412 	USB_QUIRK(SUPERTOP, IDE, 0x0000, 0xffff, UQ_MSC_IGNORE_RESIDUE,
413 	    UQ_MSC_NO_SYNC_CACHE),
414 	USB_QUIRK(SUPERTOP, FLASHDRIVE, 0x0000, 0xffff, UQ_MSC_NO_TEST_UNIT_READY,
415 	    UQ_MSC_NO_SYNC_CACHE),
416 	USB_QUIRK(TAUGA, CAMERAMATE, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
417 	USB_QUIRK(TEAC, FD05PUB, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
418 	    UQ_MSC_FORCE_PROTO_UFI),
419 	USB_QUIRK(TECLAST, TLC300, 0x0000, 0xffff, UQ_MSC_NO_TEST_UNIT_READY,
420 	    UQ_MSC_NO_SYNC_CACHE),
421 	USB_QUIRK(TREK, MEMKEY, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
422 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
423 	USB_QUIRK(TREK, THUMBDRIVE_8MB, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
424 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_IGNORE_RESIDUE),
425 	USB_QUIRK(TRUMPION, C3310, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
426 	    UQ_MSC_FORCE_PROTO_UFI),
427 	USB_QUIRK(TRUMPION, MP3, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_RBC),
428 	USB_QUIRK(TRUMPION, T33520, 0x0000, 0xffff, UQ_MSC_FORCE_PROTO_SCSI),
429 	USB_QUIRK(TWINMOS, MDIV, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
430 	    UQ_MSC_FORCE_PROTO_SCSI),
431 	USB_QUIRK(VIA, USB2IDEBRIDGE, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
432 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_SYNC_CACHE),
433 	USB_QUIRK(VIVITAR, 35XX, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
434 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
435 	USB_QUIRK(WESTERN, COMBO, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
436 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_FORCE_SHORT_INQ,
437 	    UQ_MSC_NO_START_STOP, UQ_MSC_IGNORE_RESIDUE),
438 	USB_QUIRK(WESTERN, EXTHDD, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
439 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_FORCE_SHORT_INQ,
440 	    UQ_MSC_NO_START_STOP, UQ_MSC_IGNORE_RESIDUE),
441 	USB_QUIRK(WESTERN, MYBOOK, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
442 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY_EVPD,
443 	    UQ_MSC_NO_SYNC_CACHE),
444 	USB_QUIRK(WESTERN, MYPASSPORT_00, 0x0000, 0xffff, UQ_MSC_FORCE_SHORT_INQ),
445 	USB_QUIRK(WESTERN, MYPASSPORT_01, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
446 	USB_QUIRK(WESTERN, MYPASSPORT_02, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
447 	USB_QUIRK(WESTERN, MYPASSPORT_03, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
448 	USB_QUIRK(WESTERN, MYPASSPORT_04, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
449 	USB_QUIRK(WESTERN, MYPASSPORT_05, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
450 	USB_QUIRK(WESTERN, MYPASSPORT_06, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
451 	USB_QUIRK(WESTERN, MYPASSPORT_07, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
452 	USB_QUIRK(WESTERN, MYPASSPORT_08, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
453 	USB_QUIRK(WESTERN, MYPASSPORT_09, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
454 	USB_QUIRK(WESTERN, MYPASSPORT_10, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
455 	USB_QUIRK(WESTERN, MYPASSPORT_11, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
456 	USB_QUIRK(WESTERN, MYPASSPORTES_00, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
457 	USB_QUIRK(WESTERN, MYPASSPORTES_01, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
458 	USB_QUIRK(WESTERN, MYPASSPORTES_02, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
459 	USB_QUIRK(WESTERN, MYPASSPORTES_03, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
460 	USB_QUIRK(WESTERN, MYPASSPORTES_04, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
461 	USB_QUIRK(WESTERN, MYPASSPORTES_05, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
462 	USB_QUIRK(WESTERN, MYPASSPORTES_06, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
463 	USB_QUIRK(WESTERN, MYPASSPORTES_07, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
464 	USB_QUIRK(WESTERN, MYPASSPORTES_08, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
465 	USB_QUIRK(WESTERN, MYPASSPORTES_09, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
466 	USB_QUIRK(WINMAXGROUP, FLASH64MC, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
467 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY),
468 	USB_QUIRK(YANO, FW800HD, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
469 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_FORCE_SHORT_INQ,
470 	    UQ_MSC_NO_START_STOP, UQ_MSC_IGNORE_RESIDUE),
471 	USB_QUIRK(YANO, U640MO, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI_I,
472 	    UQ_MSC_FORCE_PROTO_ATAPI, UQ_MSC_FORCE_SHORT_INQ),
473 	USB_QUIRK(YEDATA, FLASHBUSTERU, 0x0000, 0x007F, UQ_MSC_FORCE_WIRE_CBI,
474 	    UQ_MSC_FORCE_PROTO_UFI, UQ_MSC_NO_RS_CLEAR_UA, UQ_MSC_FLOPPY_SPEED,
475 	    UQ_MSC_NO_TEST_UNIT_READY, UQ_MSC_NO_GETMAXLUN),
476 	USB_QUIRK(YEDATA, FLASHBUSTERU, 0x0080, 0x0080, UQ_MSC_FORCE_WIRE_CBI_I,
477 	    UQ_MSC_FORCE_PROTO_UFI, UQ_MSC_NO_RS_CLEAR_UA, UQ_MSC_FLOPPY_SPEED,
478 	    UQ_MSC_NO_TEST_UNIT_READY, UQ_MSC_NO_GETMAXLUN),
479 	USB_QUIRK(YEDATA, FLASHBUSTERU, 0x0081, 0xFFFF, UQ_MSC_FORCE_WIRE_CBI_I,
480 	    UQ_MSC_FORCE_PROTO_UFI, UQ_MSC_NO_RS_CLEAR_UA, UQ_MSC_FLOPPY_SPEED,
481 	    UQ_MSC_NO_GETMAXLUN),
482 	USB_QUIRK(ZORAN, EX20DSC, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_CBI,
483 	    UQ_MSC_FORCE_PROTO_ATAPI),
484 	USB_QUIRK(MEIZU, M6_SL, 0x0000, 0xffff, UQ_MSC_FORCE_WIRE_BBB,
485 	    UQ_MSC_FORCE_PROTO_SCSI, UQ_MSC_NO_INQUIRY, UQ_MSC_NO_SYNC_CACHE),
486 	USB_QUIRK(TOSHIBA, TRANSMEMORY, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE,
487 	    UQ_MSC_NO_PREVENT_ALLOW),
488 	USB_QUIRK(VIALABS, USB30SATABRIDGE, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE),
489 	USB_QUIRK(QUALCOMMINC, ZTE_MF730M, 0x0000, 0xffff, UQ_MSC_NO_GETMAXLUN,
490 	    UQ_MSC_NO_INQUIRY, UQ_CFG_INDEX_0),
491 	/* Non-standard USB MIDI devices */
492 	USB_QUIRK(ROLAND, UM1, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
493 	USB_QUIRK(ROLAND, SC8850, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
494 	USB_QUIRK(ROLAND, SD90, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
495 	USB_QUIRK(ROLAND, UM880N, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
496 	USB_QUIRK(ROLAND, UA100, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
497 	USB_QUIRK(ROLAND, UM4, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
498 	USB_QUIRK(ROLAND, U8, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
499 	USB_QUIRK(ROLAND, UM2, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
500 	USB_QUIRK(ROLAND, SC8820, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
501 	USB_QUIRK(ROLAND, PC300, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
502 	USB_QUIRK(ROLAND, SK500, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
503 	USB_QUIRK(ROLAND, SCD70, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
504 	USB_QUIRK(ROLAND, UM550, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
505 	USB_QUIRK(ROLAND, SD20, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
506 	USB_QUIRK(ROLAND, SD80, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
507 	USB_QUIRK(ROLAND, UA700, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
508 	USB_QUIRK(EGO, M4U, 0x0000, 0xffff, UQ_SINGLE_CMD_MIDI),
509 	USB_QUIRK(LOGILINK, U2M, 0x0000, 0xffff, UQ_SINGLE_CMD_MIDI),
510 	USB_QUIRK(MEDELI, DD305, 0x0000, 0xffff, UQ_SINGLE_CMD_MIDI, UQ_MATCH_VENDOR_ONLY),
511 	USB_QUIRK(REDOCTANE, GHMIDI, 0x0000, 0xffff, UQ_SINGLE_CMD_MIDI),
512 	USB_QUIRK(TEXTECH, U2M_1, 0x0000, 0xffff, UQ_SINGLE_CMD_MIDI),
513 	USB_QUIRK(TEXTECH, U2M_2, 0x0000, 0xffff, UQ_SINGLE_CMD_MIDI),
514 	USB_QUIRK(WCH2, U2M, 0x0000, 0xffff, UQ_SINGLE_CMD_MIDI),
515 
516 	/* Non-standard USB AUDIO devices */
517 	USB_QUIRK(MAUDIO, FASTTRACKULTRA, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
518 	USB_QUIRK(MAUDIO, FASTTRACKULTRA8R, 0x0000, 0xffff, UQ_AU_VENDOR_CLASS),
519 
520 	/*
521 	 * Quirks for manufacturers which USB devices does not respond
522 	 * after issuing non-supported commands:
523 	 */
524 	USB_QUIRK(ALCOR, DUMMY, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE, UQ_MSC_NO_TEST_UNIT_READY, UQ_MATCH_VENDOR_ONLY),
525 	USB_QUIRK(APPLE, DUMMY, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE, UQ_MATCH_VENDOR_ONLY),
526 	USB_QUIRK(FEIYA, DUMMY, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE, UQ_MATCH_VENDOR_ONLY),
527 	USB_QUIRK(REALTEK, DUMMY, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE, UQ_MATCH_VENDOR_ONLY),
528 	USB_QUIRK(INITIO, DUMMY, 0x0000, 0xffff, UQ_MSC_NO_SYNC_CACHE, UQ_MATCH_VENDOR_ONLY),
529 
530 	/* DYMO LabelManager Pnp */
531 	USB_QUIRK(DYMO, LABELMANAGERPNP, 0x0000, 0xffff, UQ_MSC_DYMO_EJECT),
532 };
533 #undef USB_QUIRK_VP
534 #undef USB_QUIRK
535 
536 static const char *usb_quirk_str[USB_QUIRK_MAX] = {
537 	[UQ_NONE]		= "UQ_NONE",
538 	[UQ_MATCH_VENDOR_ONLY]	= "UQ_MATCH_VENDOR_ONLY",
539 	[UQ_AUDIO_SWAP_LR]	= "UQ_AUDIO_SWAP_LR",
540 	[UQ_AU_INP_ASYNC]	= "UQ_AU_INP_ASYNC",
541 	[UQ_AU_NO_FRAC]		= "UQ_AU_NO_FRAC",
542 	[UQ_AU_NO_XU]		= "UQ_AU_NO_XU",
543 	[UQ_BAD_ADC]		= "UQ_BAD_ADC",
544 	[UQ_BAD_AUDIO]		= "UQ_BAD_AUDIO",
545 	[UQ_BROKEN_BIDIR]	= "UQ_BROKEN_BIDIR",
546 	[UQ_BUS_POWERED]	= "UQ_BUS_POWERED",
547 	[UQ_HID_IGNORE]		= "UQ_HID_IGNORE",
548 	[UQ_KBD_IGNORE]		= "UQ_KBD_IGNORE",
549 	[UQ_KBD_BOOTPROTO]	= "UQ_KBD_BOOTPROTO",
550 	[UQ_UMS_IGNORE]		= "UQ_UMS_IGNORE",
551 	[UQ_MS_BAD_CLASS]	= "UQ_MS_BAD_CLASS",
552 	[UQ_MS_LEADING_BYTE]	= "UQ_MS_LEADING_BYTE",
553 	[UQ_MS_REVZ]		= "UQ_MS_REVZ",
554 	[UQ_NO_STRINGS]		= "UQ_NO_STRINGS",
555 	[UQ_POWER_CLAIM]	= "UQ_POWER_CLAIM",
556 	[UQ_SPUR_BUT_UP]	= "UQ_SPUR_BUT_UP",
557 	[UQ_SWAP_UNICODE]	= "UQ_SWAP_UNICODE",
558 	[UQ_CFG_INDEX_1]	= "UQ_CFG_INDEX_1",
559 	[UQ_CFG_INDEX_2]	= "UQ_CFG_INDEX_2",
560 	[UQ_CFG_INDEX_3]	= "UQ_CFG_INDEX_3",
561 	[UQ_CFG_INDEX_4]	= "UQ_CFG_INDEX_4",
562 	[UQ_CFG_INDEX_0]	= "UQ_CFG_INDEX_0",
563 	[UQ_ASSUME_CM_OVER_DATA]	= "UQ_ASSUME_CM_OVER_DATA",
564 	[UQ_MSC_NO_TEST_UNIT_READY]	= "UQ_MSC_NO_TEST_UNIT_READY",
565 	[UQ_MSC_NO_RS_CLEAR_UA]		= "UQ_MSC_NO_RS_CLEAR_UA",
566 	[UQ_MSC_NO_START_STOP]		= "UQ_MSC_NO_START_STOP",
567 	[UQ_MSC_NO_GETMAXLUN]		= "UQ_MSC_NO_GETMAXLUN",
568 	[UQ_MSC_NO_INQUIRY]		= "UQ_MSC_NO_INQUIRY",
569 	[UQ_MSC_NO_INQUIRY_EVPD]	= "UQ_MSC_NO_INQUIRY_EVPD",
570 	[UQ_MSC_NO_PREVENT_ALLOW]	= "UQ_MSC_NO_PREVENT_ALLOW",
571 	[UQ_MSC_NO_SYNC_CACHE]		= "UQ_MSC_NO_SYNC_CACHE",
572 	[UQ_MSC_SHUTTLE_INIT]		= "UQ_MSC_SHUTTLE_INIT",
573 	[UQ_MSC_ALT_IFACE_1]		= "UQ_MSC_ALT_IFACE_1",
574 	[UQ_MSC_FLOPPY_SPEED]		= "UQ_MSC_FLOPPY_SPEED",
575 	[UQ_MSC_IGNORE_RESIDUE]		= "UQ_MSC_IGNORE_RESIDUE",
576 	[UQ_MSC_WRONG_CSWSIG]		= "UQ_MSC_WRONG_CSWSIG",
577 	[UQ_MSC_RBC_PAD_TO_12]		= "UQ_MSC_RBC_PAD_TO_12",
578 	[UQ_MSC_READ_CAP_OFFBY1]	= "UQ_MSC_READ_CAP_OFFBY1",
579 	[UQ_MSC_FORCE_SHORT_INQ]	= "UQ_MSC_FORCE_SHORT_INQ",
580 	[UQ_MSC_FORCE_WIRE_BBB]		= "UQ_MSC_FORCE_WIRE_BBB",
581 	[UQ_MSC_FORCE_WIRE_CBI]		= "UQ_MSC_FORCE_WIRE_CBI",
582 	[UQ_MSC_FORCE_WIRE_CBI_I]	= "UQ_MSC_FORCE_WIRE_CBI_I",
583 	[UQ_MSC_FORCE_PROTO_SCSI]	= "UQ_MSC_FORCE_PROTO_SCSI",
584 	[UQ_MSC_FORCE_PROTO_ATAPI]	= "UQ_MSC_FORCE_PROTO_ATAPI",
585 	[UQ_MSC_FORCE_PROTO_UFI]	= "UQ_MSC_FORCE_PROTO_UFI",
586 	[UQ_MSC_FORCE_PROTO_RBC]	= "UQ_MSC_FORCE_PROTO_RBC",
587 	[UQ_MSC_EJECT_HUAWEI]		= "UQ_MSC_EJECT_HUAWEI",
588 	[UQ_MSC_EJECT_SIERRA]		= "UQ_MSC_EJECT_SIERRA",
589 	[UQ_MSC_EJECT_SCSIEJECT]	= "UQ_MSC_EJECT_SCSIEJECT",
590 	[UQ_MSC_EJECT_REZERO]		= "UQ_MSC_EJECT_REZERO",
591 	[UQ_MSC_EJECT_ZTESTOR]		= "UQ_MSC_EJECT_ZTESTOR",
592 	[UQ_MSC_EJECT_CMOTECH]		= "UQ_MSC_EJECT_CMOTECH",
593 	[UQ_MSC_EJECT_WAIT]		= "UQ_MSC_EJECT_WAIT",
594 	[UQ_MSC_EJECT_SAEL_M460]	= "UQ_MSC_EJECT_SAEL_M460",
595 	[UQ_MSC_EJECT_HUAWEISCSI]	= "UQ_MSC_EJECT_HUAWEISCSI",
596 	[UQ_MSC_EJECT_HUAWEISCSI2]	= "UQ_MSC_EJECT_HUAWEISCSI2",
597 	[UQ_MSC_EJECT_TCT]		= "UQ_MSC_EJECT_TCT",
598 	[UQ_BAD_MIDI]			= "UQ_BAD_MIDI",
599 	[UQ_AU_VENDOR_CLASS]		= "UQ_AU_VENDOR_CLASS",
600 	[UQ_SINGLE_CMD_MIDI]		= "UQ_SINGLE_CMD_MIDI",
601 	[UQ_MSC_DYMO_EJECT]		= "UQ_MSC_DYMO_EJECT",
602 };
603 
604 /*------------------------------------------------------------------------*
605  *	usb_quirkstr
606  *
607  * This function converts an USB quirk code into a string.
608  *------------------------------------------------------------------------*/
609 static const char *
610 usb_quirkstr(uint16_t quirk)
611 {
612 	return ((quirk < USB_QUIRK_MAX && usb_quirk_str[quirk] != NULL) ?
613 	    usb_quirk_str[quirk] : "UQ_UNKNOWN");
614 }
615 
616 /*------------------------------------------------------------------------*
617  *	usb_strquirk
618  *
619  * This function converts a string into a USB quirk code.
620  *
621  * Returns:
622  * Less than USB_QUIRK_MAX: Quirk code
623  * Else: Quirk code not found
624  *------------------------------------------------------------------------*/
625 static uint16_t
626 usb_strquirk(const char *str, size_t len)
627 {
628 	const char *quirk;
629 	uint16_t x;
630 
631 	for (x = 0; x != USB_QUIRK_MAX; x++) {
632 		quirk = usb_quirkstr(x);
633 		if (strncmp(str, quirk, len) == 0 &&
634 		    quirk[len] == 0)
635 			break;
636 	}
637 	return (x);
638 }
639 
640 /*------------------------------------------------------------------------*
641  *	usb_test_quirk_by_info
642  *
643  * Returns:
644  * 0: Quirk not found
645  * Else: Quirk found
646  *------------------------------------------------------------------------*/
647 static uint8_t
648 usb_test_quirk_by_info(const struct usbd_lookup_info *info, uint16_t quirk)
649 {
650 	uint16_t x;
651 	uint16_t y;
652 
653 	if (quirk == UQ_NONE)
654 		goto done;
655 
656 	mtx_lock(&usb_quirk_mtx);
657 
658 	for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) {
659 		/* see if quirk information does not match */
660 		if ((usb_quirks[x].vid != info->idVendor) ||
661 		    (usb_quirks[x].lo_rev > info->bcdDevice) ||
662 		    (usb_quirks[x].hi_rev < info->bcdDevice)) {
663 			continue;
664 		}
665 		/* see if quirk only should match vendor ID */
666 		if (usb_quirks[x].pid != info->idProduct) {
667 			if (usb_quirks[x].pid != 0)
668 				continue;
669 
670 			for (y = 0; y != USB_SUB_QUIRKS_MAX; y++) {
671 				if (usb_quirks[x].quirks[y] == UQ_MATCH_VENDOR_ONLY)
672 					break;
673 			}
674 			if (y == USB_SUB_QUIRKS_MAX)
675 				continue;
676 		}
677 		/* lookup quirk */
678 		for (y = 0; y != USB_SUB_QUIRKS_MAX; y++) {
679 			if (usb_quirks[x].quirks[y] == quirk) {
680 				mtx_unlock(&usb_quirk_mtx);
681 				DPRINTF("Found quirk '%s'.\n", usb_quirkstr(quirk));
682 				return (1);
683 			}
684 		}
685 		/* no quirk found */
686 		break;
687 	}
688 	mtx_unlock(&usb_quirk_mtx);
689 done:
690 	return (0);			/* no quirk match */
691 }
692 
693 static struct usb_quirk_entry *
694 usb_quirk_get_entry(uint16_t vid, uint16_t pid,
695     uint16_t lo_rev, uint16_t hi_rev, uint8_t do_alloc)
696 {
697 	uint16_t x;
698 
699 	mtx_assert(&usb_quirk_mtx, MA_OWNED);
700 
701 	if ((vid | pid | lo_rev | hi_rev) == 0) {
702 		/* all zero - special case */
703 		return (usb_quirks + USB_DEV_QUIRKS_MAX - 1);
704 	}
705 	/* search for an existing entry */
706 	for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) {
707 		/* see if quirk information does not match */
708 		if ((usb_quirks[x].vid != vid) ||
709 		    (usb_quirks[x].pid != pid) ||
710 		    (usb_quirks[x].lo_rev != lo_rev) ||
711 		    (usb_quirks[x].hi_rev != hi_rev)) {
712 			continue;
713 		}
714 		return (usb_quirks + x);
715 	}
716 
717 	if (do_alloc == 0) {
718 		/* no match */
719 		return (NULL);
720 	}
721 	/* search for a free entry */
722 	for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) {
723 		/* see if quirk information does not match */
724 		if ((usb_quirks[x].vid |
725 		    usb_quirks[x].pid |
726 		    usb_quirks[x].lo_rev |
727 		    usb_quirks[x].hi_rev) != 0) {
728 			continue;
729 		}
730 		usb_quirks[x].vid = vid;
731 		usb_quirks[x].pid = pid;
732 		usb_quirks[x].lo_rev = lo_rev;
733 		usb_quirks[x].hi_rev = hi_rev;
734 
735 		return (usb_quirks + x);
736 	}
737 
738 	/* no entry found */
739 	return (NULL);
740 }
741 
742 /*------------------------------------------------------------------------*
743  *	usb_quirk_ioctl - handle quirk IOCTLs
744  *
745  * Returns:
746  * 0: Success
747  * Else: Failure
748  *------------------------------------------------------------------------*/
749 static int
750 usb_quirk_ioctl(unsigned long cmd, caddr_t data,
751     int fflag, struct thread *td)
752 {
753 	struct usb_gen_quirk *pgq;
754 	struct usb_quirk_entry *pqe;
755 	uint32_t x;
756 	uint32_t y;
757 	int err;
758 
759 	switch (cmd) {
760 	case USB_DEV_QUIRK_GET:
761 		pgq = (void *)data;
762 		x = pgq->index % USB_SUB_QUIRKS_MAX;
763 		y = pgq->index / USB_SUB_QUIRKS_MAX;
764 		if (y >= USB_DEV_QUIRKS_MAX) {
765 			return (EINVAL);
766 		}
767 		mtx_lock(&usb_quirk_mtx);
768 		/* copy out data */
769 		pgq->vid = usb_quirks[y].vid;
770 		pgq->pid = usb_quirks[y].pid;
771 		pgq->bcdDeviceLow = usb_quirks[y].lo_rev;
772 		pgq->bcdDeviceHigh = usb_quirks[y].hi_rev;
773 		strlcpy(pgq->quirkname,
774 		    usb_quirkstr(usb_quirks[y].quirks[x]),
775 		    sizeof(pgq->quirkname));
776 		mtx_unlock(&usb_quirk_mtx);
777 		return (0);		/* success */
778 
779 	case USB_QUIRK_NAME_GET:
780 		pgq = (void *)data;
781 		x = pgq->index;
782 		if (x >= USB_QUIRK_MAX) {
783 			return (EINVAL);
784 		}
785 		strlcpy(pgq->quirkname,
786 		    usb_quirkstr(x), sizeof(pgq->quirkname));
787 		return (0);		/* success */
788 
789 	case USB_DEV_QUIRK_ADD:
790 		pgq = (void *)data;
791 
792 		/* check privileges */
793 		err = priv_check(curthread, PRIV_DRIVER);
794 		if (err) {
795 			return (err);
796 		}
797 		/* convert quirk string into numerical */
798 		for (y = 0; y != USB_DEV_QUIRKS_MAX; y++) {
799 			if (strcmp(pgq->quirkname, usb_quirkstr(y)) == 0) {
800 				break;
801 			}
802 		}
803 		if (y == USB_DEV_QUIRKS_MAX) {
804 			return (EINVAL);
805 		}
806 		if (y == UQ_NONE) {
807 			return (EINVAL);
808 		}
809 		mtx_lock(&usb_quirk_mtx);
810 		pqe = usb_quirk_get_entry(pgq->vid, pgq->pid,
811 		    pgq->bcdDeviceLow, pgq->bcdDeviceHigh, 1);
812 		if (pqe == NULL) {
813 			mtx_unlock(&usb_quirk_mtx);
814 			return (EINVAL);
815 		}
816 		for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) {
817 			if (pqe->quirks[x] == UQ_NONE) {
818 				pqe->quirks[x] = y;
819 				break;
820 			}
821 		}
822 		mtx_unlock(&usb_quirk_mtx);
823 		if (x == USB_SUB_QUIRKS_MAX) {
824 			return (ENOMEM);
825 		}
826 		return (0);		/* success */
827 
828 	case USB_DEV_QUIRK_REMOVE:
829 		pgq = (void *)data;
830 		/* check privileges */
831 		err = priv_check(curthread, PRIV_DRIVER);
832 		if (err) {
833 			return (err);
834 		}
835 		/* convert quirk string into numerical */
836 		for (y = 0; y != USB_DEV_QUIRKS_MAX; y++) {
837 			if (strcmp(pgq->quirkname, usb_quirkstr(y)) == 0) {
838 				break;
839 			}
840 		}
841 		if (y == USB_DEV_QUIRKS_MAX) {
842 			return (EINVAL);
843 		}
844 		if (y == UQ_NONE) {
845 			return (EINVAL);
846 		}
847 		mtx_lock(&usb_quirk_mtx);
848 		pqe = usb_quirk_get_entry(pgq->vid, pgq->pid,
849 		    pgq->bcdDeviceLow, pgq->bcdDeviceHigh, 0);
850 		if (pqe == NULL) {
851 			mtx_unlock(&usb_quirk_mtx);
852 			return (EINVAL);
853 		}
854 		for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) {
855 			if (pqe->quirks[x] == y) {
856 				pqe->quirks[x] = UQ_NONE;
857 				break;
858 			}
859 		}
860 		if (x == USB_SUB_QUIRKS_MAX) {
861 			mtx_unlock(&usb_quirk_mtx);
862 			return (ENOMEM);
863 		}
864 		for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) {
865 			if (pqe->quirks[x] != UQ_NONE) {
866 				break;
867 			}
868 		}
869 		if (x == USB_SUB_QUIRKS_MAX) {
870 			/* all quirk entries are unused - release */
871 			memset(pqe, 0, sizeof(*pqe));
872 		}
873 		mtx_unlock(&usb_quirk_mtx);
874 		return (0);		/* success */
875 
876 	default:
877 		break;
878 	}
879 	return (ENOIOCTL);
880 }
881 
882 /*------------------------------------------------------------------------*
883  *	usb_quirk_strtou16
884  *
885  * Helper function to scan a 16-bit integer.
886  *------------------------------------------------------------------------*/
887 static uint16_t
888 usb_quirk_strtou16(const char **pptr, const char *name, const char *what)
889 {
890 	unsigned long value;
891 	char *end;
892 
893 	value = strtoul(*pptr, &end, 0);
894 	if (value > 65535 || *pptr == end || (*end != ' ' && *end != '\t')) {
895 		printf("%s: %s 16-bit %s value set to zero\n",
896 		    name, what, *end == 0 ? "incomplete" : "invalid");
897 		return (0);
898 	}
899 	*pptr = end + 1;
900 	return ((uint16_t)value);
901 }
902 
903 /*------------------------------------------------------------------------*
904  *	usb_quirk_add_entry_from_str
905  *
906  * Add a USB quirk entry from string.
907  *     "VENDOR PRODUCT LO_REV HI_REV QUIRK[,QUIRK[,...]]"
908  *------------------------------------------------------------------------*/
909 static void
910 usb_quirk_add_entry_from_str(const char *name, const char *env)
911 {
912 	struct usb_quirk_entry entry = { };
913 	struct usb_quirk_entry *new;
914 	uint16_t quirk_idx;
915 	uint16_t quirk;
916 	const char *end;
917 
918 	/* check for invalid environment variable */
919 	if (name == NULL || env == NULL)
920 		return;
921 
922 	if (bootverbose)
923 		printf("Adding USB QUIRK '%s' = '%s'\n", name, env);
924 
925 	/* parse device information */
926 	entry.vid = usb_quirk_strtou16(&env, name, "Vendor ID");
927 	entry.pid = usb_quirk_strtou16(&env, name, "Product ID");
928 	entry.lo_rev = usb_quirk_strtou16(&env, name, "Low revision");
929 	entry.hi_rev = usb_quirk_strtou16(&env, name, "High revision");
930 
931 	/* parse quirk information */
932 	quirk_idx = 0;
933 	while (*env != 0 && quirk_idx != USB_SUB_QUIRKS_MAX) {
934 		/* skip whitespace before quirks */
935 		while (*env == ' ' || *env == '\t')
936 			env++;
937 
938 		/* look for quirk separation character */
939 		end = strchr(env, ',');
940 		if (end == NULL)
941 			end = env + strlen(env);
942 
943 		/* lookup quirk in string table */
944 		quirk = usb_strquirk(env, end - env);
945 		if (quirk < USB_QUIRK_MAX) {
946 			entry.quirks[quirk_idx++] = quirk;
947 		} else {
948 			printf("%s: unknown USB quirk '%.*s' (skipped)\n",
949 			    name, (int)(end - env), env);
950 		}
951 		env = end;
952 
953 		/* skip quirk delimiter, if any */
954 		if (*env != 0)
955 			env++;
956 	}
957 
958 	/* register quirk */
959 	if (quirk_idx != 0) {
960 		if (*env != 0) {
961 			printf("%s: Too many USB quirks, only %d allowed!\n",
962 			    name, USB_SUB_QUIRKS_MAX);
963 		}
964 		mtx_lock(&usb_quirk_mtx);
965 		new = usb_quirk_get_entry(entry.vid, entry.pid,
966 		    entry.lo_rev, entry.hi_rev, 1);
967 		if (new == NULL)
968 			printf("%s: USB quirks table is full!\n", name);
969 		else
970 			memcpy(new->quirks, entry.quirks, sizeof(entry.quirks));
971 		mtx_unlock(&usb_quirk_mtx);
972 	} else {
973 		printf("%s: No USB quirks found!\n", name);
974 	}
975 }
976 
977 static void
978 usb_quirk_init(void *arg)
979 {
980 	char envkey[sizeof(USB_QUIRK_ENVROOT) + 2];	/* 2 digits max, 0 to 99 */
981 	int i;
982 
983 	/* initialize mutex */
984 	mtx_init(&usb_quirk_mtx, "USB quirk", NULL, MTX_DEF);
985 
986 	/* look for quirks defined by the environment variable */
987 	for (i = 0; i != 100; i++) {
988 		snprintf(envkey, sizeof(envkey), USB_QUIRK_ENVROOT "%d", i);
989 
990 		/* Stop at first undefined var */
991 		if (!testenv(envkey))
992 			break;
993 
994 		/* parse environment variable */
995 		usb_quirk_add_entry_from_str(envkey, kern_getenv(envkey));
996 	}
997 
998 	/* register our function */
999 	usb_test_quirk_p = &usb_test_quirk_by_info;
1000 	usb_quirk_ioctl_p = &usb_quirk_ioctl;
1001 }
1002 
1003 static void
1004 usb_quirk_uninit(void *arg)
1005 {
1006 	usb_quirk_unload(arg);
1007 
1008 	/* destroy mutex */
1009 	mtx_destroy(&usb_quirk_mtx);
1010 }
1011 
1012 SYSINIT(usb_quirk_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_quirk_init, NULL);
1013 SYSUNINIT(usb_quirk_uninit, SI_SUB_LOCK, SI_ORDER_ANY, usb_quirk_uninit, NULL);
1014