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