usb_quirk.c revision 188074
1/* $FreeBSD: head/sys/dev/usb2/quirk/usb2_quirk.c 188074 2009-02-03 15:24:00Z thompsa $ */ 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 <dev/usb2/include/usb2_standard.h> 30#include <dev/usb2/include/usb2_ioctl.h> 31#include <dev/usb2/include/usb2_mfunc.h> 32#include <dev/usb2/include/usb2_devid.h> 33 34#define USB_DEBUG_VAR usb2_debug 35 36#include <dev/usb2/core/usb2_core.h> 37#include <dev/usb2/core/usb2_lookup.h> 38#include <dev/usb2/core/usb2_debug.h> 39#include <dev/usb2/core/usb2_dynamic.h> 40 41#include <dev/usb2/quirk/usb2_quirk.h> 42 43MODULE_DEPEND(usb2_quirk, usb2_core, 1, 1, 1); 44MODULE_VERSION(usb2_quirk, 1); 45 46/* 47 * The following macro adds one or more quirks for a USB device: 48 */ 49#define USB_QUIRK_ENTRY(v,p,l,h,...) \ 50 .vid = (v), .pid = (p), .lo_rev = (l), .hi_rev = (h), .quirks = { __VA_ARGS__ } 51 52#define USB_DEV_QUIRKS_MAX 128 53#define USB_SUB_QUIRKS_MAX 8 54 55struct usb2_quirk_entry { 56 uint16_t vid; 57 uint16_t pid; 58 uint16_t lo_rev; 59 uint16_t hi_rev; 60 uint16_t quirks[USB_SUB_QUIRKS_MAX]; 61}; 62 63static struct mtx usb2_quirk_mtx; 64 65static struct usb2_quirk_entry usb2_quirks[USB_DEV_QUIRKS_MAX] = { 66 {USB_QUIRK_ENTRY(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_LCM, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 67 {USB_QUIRK_ENTRY(USB_VENDOR_INSIDEOUT, USB_PRODUCT_INSIDEOUT_EDGEPORT4, 0x094, 0x094, UQ_SWAP_UNICODE, UQ_NONE)}, 68 {USB_QUIRK_ENTRY(USB_VENDOR_DALLAS, USB_PRODUCT_DALLAS_J6502, 0x0a2, 0x0a2, UQ_BAD_ADC, UQ_NONE)}, 69 {USB_QUIRK_ENTRY(USB_VENDOR_DALLAS, USB_PRODUCT_DALLAS_J6502, 0x0a2, 0x0a2, UQ_AU_NO_XU, UQ_NONE)}, 70 {USB_QUIRK_ENTRY(USB_VENDOR_ALTEC, USB_PRODUCT_ALTEC_ADA70, 0x103, 0x103, UQ_BAD_ADC, UQ_NONE)}, 71 {USB_QUIRK_ENTRY(USB_VENDOR_ALTEC, USB_PRODUCT_ALTEC_ASC495, 0x000, 0x000, UQ_BAD_AUDIO, UQ_NONE)}, 72 {USB_QUIRK_ENTRY(USB_VENDOR_QTRONIX, USB_PRODUCT_QTRONIX_980N, 0x110, 0x110, UQ_SPUR_BUT_UP, UQ_NONE)}, 73 {USB_QUIRK_ENTRY(USB_VENDOR_ALCOR2, USB_PRODUCT_ALCOR2_KBD_HUB, 0x001, 0x001, UQ_SPUR_BUT_UP, UQ_NONE)}, 74 {USB_QUIRK_ENTRY(USB_VENDOR_MCT, USB_PRODUCT_MCT_HUB0100, 0x102, 0x102, UQ_BUS_POWERED, UQ_NONE)}, 75 {USB_QUIRK_ENTRY(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0x102, 0x102, UQ_BUS_POWERED, UQ_NONE)}, 76 {USB_QUIRK_ENTRY(USB_VENDOR_TI, USB_PRODUCT_TI_UTUSB41, 0x110, 0x110, UQ_POWER_CLAIM, UQ_NONE)}, 77 {USB_QUIRK_ENTRY(USB_VENDOR_TELEX, USB_PRODUCT_TELEX_MIC1, 0x009, 0x009, UQ_AU_NO_FRAC, UQ_NONE)}, 78 {USB_QUIRK_ENTRY(USB_VENDOR_SILICONPORTALS, USB_PRODUCT_SILICONPORTALS_YAPPHONE, 0x100, 0x100, UQ_AU_INP_ASYNC, UQ_NONE)}, 79 {USB_QUIRK_ENTRY(USB_VENDOR_LOGITECH, USB_PRODUCT_LOGITECH_UN53B, 0x0000, 0xFFFF, UQ_NO_STRINGS, UQ_NONE)}, 80 81 /* 82 * XXX The following quirks should have a more specific revision 83 * number: 84 */ 85 {USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_895C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)}, 86 {USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_880C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)}, 87 {USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_815C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)}, 88 {USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_810C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)}, 89 {USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_830C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)}, 90 {USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_1220C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)}, 91 {USB_QUIRK_ENTRY(USB_VENDOR_XEROX, USB_PRODUCT_XEROX_WCM15, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)}, 92 /* Devices which should be ignored by uhid */ 93 {USB_QUIRK_ENTRY(USB_VENDOR_APC, USB_PRODUCT_APC_UPS, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 94 {USB_QUIRK_ENTRY(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F6C550AVR, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 95 {USB_QUIRK_ENTRY(USB_VENDOR_CYBERPOWER, USB_PRODUCT_CYBERPOWER_1500CAVRLCD, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 96 {USB_QUIRK_ENTRY(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 97 {USB_QUIRK_ENTRY(USB_VENDOR_ITUNERNET, USB_PRODUCT_ITUNERNET_USBLCD2X20, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 98 {USB_QUIRK_ENTRY(USB_VENDOR_ITUNERNET, USB_PRODUCT_ITUNERNET_USBLCD4X20, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 99 {USB_QUIRK_ENTRY(USB_VENDOR_MGE, USB_PRODUCT_MGE_UPS1, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 100 {USB_QUIRK_ENTRY(USB_VENDOR_MGE, USB_PRODUCT_MGE_UPS2, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 101 {USB_QUIRK_ENTRY(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 102 {USB_QUIRK_ENTRY(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)}, 103 /* Devices which should be ignored by both ukbd and uhid */ 104 {USB_QUIRK_ENTRY(USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_WISPY1A, 0x0000, 0xFFFF, UQ_KBD_IGNORE, UQ_HID_IGNORE, UQ_NONE)}, 105 {USB_QUIRK_ENTRY(USB_VENDOR_METAGEEK, USB_PRODUCT_METAGEEK_WISPY1B, 0x0000, 0xFFFF, UQ_KBD_IGNORE, UQ_HID_IGNORE, UQ_NONE)}, 106 {USB_QUIRK_ENTRY(USB_VENDOR_TENX, USB_PRODUCT_TENX_UAUDIO0, 0x0101, 0x0101, UQ_AUDIO_SWAP_LR, UQ_NONE)}, 107 /* MS keyboards do weird things */ 108 {USB_QUIRK_ENTRY(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_WLNOTEBOOK, 0x0000, 0xFFFF, UQ_MS_BAD_CLASS, UQ_MS_LEADING_BYTE, UQ_NONE)}, 109 {USB_QUIRK_ENTRY(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_WLNOTEBOOK2, 0x0000, 0xFFFF, UQ_MS_BAD_CLASS, UQ_MS_LEADING_BYTE, UQ_NONE)}, 110 {USB_QUIRK_ENTRY(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_WLINTELLIMOUSE, 0x0000, 0xFFFF, UQ_MS_LEADING_BYTE, UQ_NONE)}, 111 {USB_QUIRK_ENTRY(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_COMFORT3000, 0x0000, 0xFFFF, UQ_MS_BAD_CLASS, UQ_MS_LEADING_BYTE, UQ_NONE)}, 112 {USB_QUIRK_ENTRY(USB_VENDOR_METAGEEK, USB_PRODUCT_METAGEEK_WISPY24X, 0x0000, 0xFFFF, UQ_KBD_IGNORE, UQ_HID_IGNORE, UQ_NONE)}, 113}; 114 115static const char *usb_quirk_str[USB_QUIRK_MAX] = { 116 "UQ_NONE", 117 "UQ_AUDIO_SWAP_LR", /* left and right sound channels are swapped */ 118 "UQ_AU_INP_ASYNC", /* input is async despite claim of adaptive */ 119 "UQ_AU_NO_FRAC", /* don't adjust for fractional samples */ 120 "UQ_AU_NO_XU", /* audio device has broken extension unit */ 121 "UQ_BAD_ADC", /* bad audio spec version number */ 122 "UQ_BAD_AUDIO", /* device claims audio class, but isn't */ 123 "UQ_BROKEN_BIDIR", /* printer has broken bidir mode */ 124 "UQ_BUS_POWERED", /* device is bus powered, despite claim */ 125 "UQ_HID_IGNORE", /* device should be ignored by hid class */ 126 "UQ_KBD_IGNORE", /* device should be ignored by kbd class */ 127 "UQ_MS_BAD_CLASS", /* doesn't identify properly */ 128 "UQ_MS_LEADING_BYTE", /* mouse sends an unknown leading byte */ 129 "UQ_MS_REVZ", /* mouse has Z-axis reversed */ 130 "UQ_NO_STRINGS", /* string descriptors are broken */ 131 "UQ_OPEN_CLEARSTALL", /* device needs clear endpoint stall */ 132 "UQ_POWER_CLAIM", /* hub lies about power status */ 133 "UQ_SPUR_BUT_UP", /* spurious mouse button up events */ 134 "UQ_SWAP_UNICODE", /* has some Unicode strings swapped */ 135 "UQ_CFG_INDEX_1", /* select configuration index 1 by default */ 136 "UQ_CFG_INDEX_2", /* select configuration index 2 by default */ 137 "UQ_CFG_INDEX_3", /* select configuration index 3 by default */ 138 "UQ_CFG_INDEX_4", /* select configuration index 4 by default */ 139 "UQ_CFG_INDEX_0", /* select configuration index 0 by default */ 140}; 141 142/*------------------------------------------------------------------------* 143 * usb2_quirkstr 144 * 145 * This function converts an USB quirk code into a string. 146 *------------------------------------------------------------------------*/ 147static const char * 148usb2_quirkstr(uint16_t quirk) 149{ 150 return ((quirk < USB_QUIRK_MAX) ? 151 usb_quirk_str[quirk] : "USB_QUIRK_UNKNOWN"); 152} 153 154/*------------------------------------------------------------------------* 155 * usb2_test_quirk_by_info 156 * 157 * Returns: 158 * 0: Quirk not found 159 * Else: Quirk found 160 *------------------------------------------------------------------------*/ 161static uint8_t 162usb2_test_quirk_by_info(const struct usb2_lookup_info *info, uint16_t quirk) 163{ 164 uint16_t x; 165 uint16_t y; 166 167 if (quirk == UQ_NONE) { 168 return (0); 169 } 170 mtx_lock(&usb2_quirk_mtx); 171 172 for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) { 173 /* see if quirk information does not match */ 174 if ((usb2_quirks[x].vid != info->idVendor) || 175 (usb2_quirks[x].pid != info->idProduct) || 176 (usb2_quirks[x].lo_rev > info->bcdDevice) || 177 (usb2_quirks[x].hi_rev < info->bcdDevice)) { 178 continue; 179 } 180 /* lookup quirk */ 181 for (y = 0; y != USB_SUB_QUIRKS_MAX; y++) { 182 if (usb2_quirks[x].quirks[y] == quirk) { 183 mtx_unlock(&usb2_quirk_mtx); 184 DPRINTF("Found quirk '%s'.\n", usb2_quirkstr(quirk)); 185 return (1); 186 } 187 } 188 /* no quirk found */ 189 break; 190 } 191 mtx_unlock(&usb2_quirk_mtx); 192 return (0); 193} 194 195static struct usb2_quirk_entry * 196usb2_quirk_get_entry(uint16_t vid, uint16_t pid, 197 uint16_t lo_rev, uint16_t hi_rev, uint8_t do_alloc) 198{ 199 uint16_t x; 200 201 mtx_assert(&usb2_quirk_mtx, MA_OWNED); 202 203 if ((vid | pid | lo_rev | hi_rev) == 0) { 204 /* all zero - special case */ 205 return (usb2_quirks + USB_DEV_QUIRKS_MAX - 1); 206 } 207 /* search for an existing entry */ 208 for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) { 209 /* see if quirk information does not match */ 210 if ((usb2_quirks[x].vid != vid) || 211 (usb2_quirks[x].pid != pid) || 212 (usb2_quirks[x].lo_rev != lo_rev) || 213 (usb2_quirks[x].hi_rev != hi_rev)) { 214 continue; 215 } 216 return (usb2_quirks + x); 217 } 218 219 if (do_alloc == 0) { 220 /* no match */ 221 return (NULL); 222 } 223 /* search for a free entry */ 224 for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) { 225 /* see if quirk information does not match */ 226 if ((usb2_quirks[x].vid | 227 usb2_quirks[x].pid | 228 usb2_quirks[x].lo_rev | 229 usb2_quirks[x].hi_rev) != 0) { 230 continue; 231 } 232 usb2_quirks[x].vid = vid; 233 usb2_quirks[x].pid = pid; 234 usb2_quirks[x].lo_rev = lo_rev; 235 usb2_quirks[x].hi_rev = hi_rev; 236 237 return (usb2_quirks + x); 238 } 239 240 /* no entry found */ 241 return (NULL); 242} 243 244/*------------------------------------------------------------------------* 245 * usb2_quirk_ioctl - handle quirk IOCTLs 246 * 247 * Returns: 248 * 0: Success 249 * Else: Failure 250 *------------------------------------------------------------------------*/ 251static int 252usb2_quirk_ioctl(unsigned long cmd, caddr_t data, 253 int fflag, struct thread *td) 254{ 255 struct usb2_gen_quirk *pgq; 256 struct usb2_quirk_entry *pqe; 257 uint32_t x; 258 uint32_t y; 259 int err; 260 261 switch (cmd) { 262 case USB_DEV_QUIRK_GET: 263 pgq = (void *)data; 264 x = pgq->index % USB_SUB_QUIRKS_MAX; 265 y = pgq->index / USB_SUB_QUIRKS_MAX; 266 if (y >= USB_DEV_QUIRKS_MAX) { 267 return (EINVAL); 268 } 269 mtx_lock(&usb2_quirk_mtx); 270 /* copy out data */ 271 pgq->vid = usb2_quirks[y].vid; 272 pgq->pid = usb2_quirks[y].pid; 273 pgq->bcdDeviceLow = usb2_quirks[y].lo_rev; 274 pgq->bcdDeviceHigh = usb2_quirks[y].hi_rev; 275 strlcpy(pgq->quirkname, 276 usb2_quirkstr(usb2_quirks[y].quirks[x]), 277 sizeof(pgq->quirkname)); 278 mtx_unlock(&usb2_quirk_mtx); 279 return (0); /* success */ 280 281 case USB_QUIRK_NAME_GET: 282 pgq = (void *)data; 283 x = pgq->index; 284 if (x >= USB_QUIRK_MAX) { 285 return (EINVAL); 286 } 287 strlcpy(pgq->quirkname, 288 usb2_quirkstr(x), sizeof(pgq->quirkname)); 289 return (0); /* success */ 290 291 case USB_DEV_QUIRK_ADD: 292 pgq = (void *)data; 293 294 /* check privileges */ 295 err = priv_check(curthread, PRIV_DRIVER); 296 if (err) { 297 return (err); 298 } 299 /* convert quirk string into numerical */ 300 for (y = 0; y != USB_DEV_QUIRKS_MAX; y++) { 301 if (strcmp(pgq->quirkname, usb2_quirkstr(y)) == 0) { 302 break; 303 } 304 } 305 if (y == USB_DEV_QUIRKS_MAX) { 306 return (EINVAL); 307 } 308 if (y == UQ_NONE) { 309 return (EINVAL); 310 } 311 mtx_lock(&usb2_quirk_mtx); 312 pqe = usb2_quirk_get_entry(pgq->vid, pgq->pid, 313 pgq->bcdDeviceLow, pgq->bcdDeviceHigh, 1); 314 for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) { 315 if (pqe->quirks[x] == UQ_NONE) { 316 pqe->quirks[x] = y; 317 break; 318 } 319 } 320 mtx_unlock(&usb2_quirk_mtx); 321 if (x == USB_SUB_QUIRKS_MAX) { 322 return (ENOMEM); 323 } 324 return (0); /* success */ 325 326 case USB_DEV_QUIRK_REMOVE: 327 pgq = (void *)data; 328 /* check privileges */ 329 err = priv_check(curthread, PRIV_DRIVER); 330 if (err) { 331 return (err); 332 } 333 /* convert quirk string into numerical */ 334 for (y = 0; y != USB_DEV_QUIRKS_MAX; y++) { 335 if (strcmp(pgq->quirkname, usb2_quirkstr(y)) == 0) { 336 break; 337 } 338 } 339 if (y == USB_DEV_QUIRKS_MAX) { 340 return (EINVAL); 341 } 342 if (y == UQ_NONE) { 343 return (EINVAL); 344 } 345 mtx_lock(&usb2_quirk_mtx); 346 pqe = usb2_quirk_get_entry(pgq->vid, pgq->pid, 347 pgq->bcdDeviceLow, pgq->bcdDeviceHigh, 0); 348 for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) { 349 if (pqe->quirks[x] == y) { 350 pqe->quirks[x] = UQ_NONE; 351 break; 352 } 353 } 354 if (x == USB_SUB_QUIRKS_MAX) { 355 mtx_unlock(&usb2_quirk_mtx); 356 return (ENOMEM); 357 } 358 for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) { 359 if (pqe->quirks[x] != UQ_NONE) { 360 break; 361 } 362 } 363 if (x == USB_SUB_QUIRKS_MAX) { 364 /* all quirk entries are unused - release */ 365 memset(pqe, 0, sizeof(pqe)); 366 } 367 mtx_unlock(&usb2_quirk_mtx); 368 return (0); /* success */ 369 370 default: 371 break; 372 } 373 return (ENOIOCTL); 374} 375 376static void 377usb2_quirk_init(void *arg) 378{ 379 /* initialize mutex */ 380 mtx_init(&usb2_quirk_mtx, "USB quirk", NULL, MTX_DEF); 381 382 /* register our function */ 383 usb2_test_quirk_p = &usb2_test_quirk_by_info; 384 usb2_quirk_ioctl_p = &usb2_quirk_ioctl; 385} 386 387static void 388usb2_quirk_uninit(void *arg) 389{ 390 usb2_quirk_unload(arg); 391 392 /* destroy mutex */ 393 mtx_destroy(&usb2_quirk_mtx); 394} 395 396SYSINIT(usb2_quirk_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb2_quirk_init, NULL); 397SYSUNINIT(usb2_quirk_uninit, SI_SUB_LOCK, SI_ORDER_ANY, usb2_quirk_uninit, NULL); 398