usb_quirk.c revision 197569
1/* $FreeBSD: head/sys/dev/usb/quirk/usb_quirk.c 197569 2009-09-28 08:09:11Z 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 <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/linker_set.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
60MODULE_DEPEND(usb_quirk, usb, 1, 1, 1);
61MODULE_VERSION(usb_quirk, 1);
62
63/*
64 * The following macro adds one or more quirks for a USB device:
65 */
66#define	USB_QUIRK_ENTRY(v,p,l,h,...) \
67  .vid = (v), .pid = (p), .lo_rev = (l), .hi_rev = (h), .quirks = { __VA_ARGS__ }
68
69#define	USB_DEV_QUIRKS_MAX 128
70#define	USB_SUB_QUIRKS_MAX 8
71
72struct usb_quirk_entry {
73	uint16_t vid;
74	uint16_t pid;
75	uint16_t lo_rev;
76	uint16_t hi_rev;
77	uint16_t quirks[USB_SUB_QUIRKS_MAX];
78};
79
80static struct mtx usb_quirk_mtx;
81
82static struct usb_quirk_entry usb_quirks[USB_DEV_QUIRKS_MAX] = {
83	{USB_QUIRK_ENTRY(USB_VENDOR_ASUS, USB_PRODUCT_ASUS_LCM, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
84	{USB_QUIRK_ENTRY(USB_VENDOR_INSIDEOUT, USB_PRODUCT_INSIDEOUT_EDGEPORT4, 0x094, 0x094, UQ_SWAP_UNICODE, UQ_NONE)},
85	{USB_QUIRK_ENTRY(USB_VENDOR_DALLAS, USB_PRODUCT_DALLAS_J6502, 0x0a2, 0x0a2, UQ_BAD_ADC, UQ_NONE)},
86	{USB_QUIRK_ENTRY(USB_VENDOR_DALLAS, USB_PRODUCT_DALLAS_J6502, 0x0a2, 0x0a2, UQ_AU_NO_XU, UQ_NONE)},
87	{USB_QUIRK_ENTRY(USB_VENDOR_ALTEC, USB_PRODUCT_ALTEC_ADA70, 0x103, 0x103, UQ_BAD_ADC, UQ_NONE)},
88	{USB_QUIRK_ENTRY(USB_VENDOR_ALTEC, USB_PRODUCT_ALTEC_ASC495, 0x000, 0x000, UQ_BAD_AUDIO, UQ_NONE)},
89	{USB_QUIRK_ENTRY(USB_VENDOR_QTRONIX, USB_PRODUCT_QTRONIX_980N, 0x110, 0x110, UQ_SPUR_BUT_UP, UQ_NONE)},
90	{USB_QUIRK_ENTRY(USB_VENDOR_ALCOR2, USB_PRODUCT_ALCOR2_KBD_HUB, 0x001, 0x001, UQ_SPUR_BUT_UP, UQ_NONE)},
91	{USB_QUIRK_ENTRY(USB_VENDOR_MCT, USB_PRODUCT_MCT_HUB0100, 0x102, 0x102, UQ_BUS_POWERED, UQ_NONE)},
92	{USB_QUIRK_ENTRY(USB_VENDOR_MCT, USB_PRODUCT_MCT_USB232, 0x102, 0x102, UQ_BUS_POWERED, UQ_NONE)},
93	{USB_QUIRK_ENTRY(USB_VENDOR_TI, USB_PRODUCT_TI_UTUSB41, 0x110, 0x110, UQ_POWER_CLAIM, UQ_NONE)},
94	{USB_QUIRK_ENTRY(USB_VENDOR_TELEX, USB_PRODUCT_TELEX_MIC1, 0x009, 0x009, UQ_AU_NO_FRAC, UQ_NONE)},
95	{USB_QUIRK_ENTRY(USB_VENDOR_SILICONPORTALS, USB_PRODUCT_SILICONPORTALS_YAPPHONE, 0x100, 0x100, UQ_AU_INP_ASYNC, UQ_NONE)},
96	{USB_QUIRK_ENTRY(USB_VENDOR_LOGITECH, USB_PRODUCT_LOGITECH_UN53B, 0x0000, 0xFFFF, UQ_NO_STRINGS, UQ_NONE)},
97	{USB_QUIRK_ENTRY(USB_VENDOR_ELSA, USB_PRODUCT_ELSA_MODEM1, 0x0000, 0xFFFF, UQ_CFG_INDEX_1, UQ_NONE)},
98
99	/*
100	 * XXX The following quirks should have a more specific revision
101	 * number:
102	 */
103	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_895C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
104	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_880C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
105	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_815C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
106	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_810C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
107	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_830C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
108	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_1220C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
109	{USB_QUIRK_ENTRY(USB_VENDOR_XEROX, USB_PRODUCT_XEROX_WCM15, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
110	/* Devices which should be ignored by uhid */
111	{USB_QUIRK_ENTRY(USB_VENDOR_APC, USB_PRODUCT_APC_UPS, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
112	{USB_QUIRK_ENTRY(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F6C550AVR, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
113	{USB_QUIRK_ENTRY(USB_VENDOR_CYBERPOWER, USB_PRODUCT_CYBERPOWER_1500CAVRLCD, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
114	{USB_QUIRK_ENTRY(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
115	{USB_QUIRK_ENTRY(USB_VENDOR_ITUNERNET, USB_PRODUCT_ITUNERNET_USBLCD2X20, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
116	{USB_QUIRK_ENTRY(USB_VENDOR_ITUNERNET, USB_PRODUCT_ITUNERNET_USBLCD4X20, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
117	{USB_QUIRK_ENTRY(USB_VENDOR_MGE, USB_PRODUCT_MGE_UPS1, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
118	{USB_QUIRK_ENTRY(USB_VENDOR_MGE, USB_PRODUCT_MGE_UPS2, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
119	{USB_QUIRK_ENTRY(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
120	{USB_QUIRK_ENTRY(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
121	/* Devices which should be ignored by both ukbd and uhid */
122	{USB_QUIRK_ENTRY(USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_WISPY1A, 0x0000, 0xFFFF, UQ_KBD_IGNORE, UQ_HID_IGNORE, UQ_NONE)},
123	{USB_QUIRK_ENTRY(USB_VENDOR_METAGEEK, USB_PRODUCT_METAGEEK_WISPY1B, 0x0000, 0xFFFF, UQ_KBD_IGNORE, UQ_HID_IGNORE, UQ_NONE)},
124	{USB_QUIRK_ENTRY(USB_VENDOR_TENX, USB_PRODUCT_TENX_UAUDIO0, 0x0101, 0x0101, UQ_AUDIO_SWAP_LR, UQ_NONE)},
125	/* MS keyboards do weird things */
126	{USB_QUIRK_ENTRY(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_WLINTELLIMOUSE, 0x0000, 0xFFFF, UQ_MS_LEADING_BYTE, UQ_NONE)},
127	{USB_QUIRK_ENTRY(USB_VENDOR_METAGEEK, USB_PRODUCT_METAGEEK_WISPY24X, 0x0000, 0xFFFF, UQ_KBD_IGNORE, UQ_HID_IGNORE, UQ_NONE)},
128};
129
130static const char *usb_quirk_str[USB_QUIRK_MAX] = {
131	[UQ_NONE]		= "UQ_NONE",
132	[UQ_AUDIO_SWAP_LR]	= "UQ_AUDIO_SWAP_LR",
133	[UQ_AU_INP_ASYNC]	= "UQ_AU_INP_ASYNC",
134	[UQ_AU_NO_FRAC]		= "UQ_AU_NO_FRAC",
135	[UQ_AU_NO_XU]		= "UQ_AU_NO_XU",
136	[UQ_BAD_ADC]		= "UQ_BAD_ADC",
137	[UQ_BAD_AUDIO]		= "UQ_BAD_AUDIO",
138	[UQ_BROKEN_BIDIR]	= "UQ_BROKEN_BIDIR",
139	[UQ_BUS_POWERED]	= "UQ_BUS_POWERED",
140	[UQ_HID_IGNORE]		= "UQ_HID_IGNORE",
141	[UQ_KBD_IGNORE]		= "UQ_KBD_IGNORE",
142	[UQ_MS_BAD_CLASS]	= "UQ_MS_BAD_CLASS",
143	[UQ_MS_LEADING_BYTE]	= "UQ_MS_LEADING_BYTE",
144	[UQ_MS_REVZ]		= "UQ_MS_REVZ",
145	[UQ_NO_STRINGS]		= "UQ_NO_STRINGS",
146	[UQ_OPEN_CLEARSTALL]	= "UQ_OPEN_CLEARSTALL",
147	[UQ_POWER_CLAIM]	= "UQ_POWER_CLAIM",
148	[UQ_SPUR_BUT_UP]	= "UQ_SPUR_BUT_UP",
149	[UQ_SWAP_UNICODE]	= "UQ_SWAP_UNICODE",
150	[UQ_CFG_INDEX_1]	= "UQ_CFG_INDEX_1",
151	[UQ_CFG_INDEX_2]	= "UQ_CFG_INDEX_2",
152	[UQ_CFG_INDEX_3]	= "UQ_CFG_INDEX_3",
153	[UQ_CFG_INDEX_4]	= "UQ_CFG_INDEX_4",
154	[UQ_CFG_INDEX_0]	= "UQ_CFG_INDEX_0",
155};
156
157/*------------------------------------------------------------------------*
158 *	usb_quirkstr
159 *
160 * This function converts an USB quirk code into a string.
161 *------------------------------------------------------------------------*/
162static const char *
163usb_quirkstr(uint16_t quirk)
164{
165	return ((quirk < USB_QUIRK_MAX) ?
166	    usb_quirk_str[quirk] : "USB_QUIRK_UNKNOWN");
167}
168
169/*------------------------------------------------------------------------*
170 *	usb_test_quirk_by_info
171 *
172 * Returns:
173 * 0: Quirk not found
174 * Else: Quirk found
175 *------------------------------------------------------------------------*/
176static uint8_t
177usb_test_quirk_by_info(const struct usbd_lookup_info *info, uint16_t quirk)
178{
179	uint16_t x;
180	uint16_t y;
181
182	if (quirk == UQ_NONE) {
183		return (0);
184	}
185	mtx_lock(&usb_quirk_mtx);
186
187	for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) {
188		/* see if quirk information does not match */
189		if ((usb_quirks[x].vid != info->idVendor) ||
190		    (usb_quirks[x].pid != info->idProduct) ||
191		    (usb_quirks[x].lo_rev > info->bcdDevice) ||
192		    (usb_quirks[x].hi_rev < info->bcdDevice)) {
193			continue;
194		}
195		/* lookup quirk */
196		for (y = 0; y != USB_SUB_QUIRKS_MAX; y++) {
197			if (usb_quirks[x].quirks[y] == quirk) {
198				mtx_unlock(&usb_quirk_mtx);
199				DPRINTF("Found quirk '%s'.\n", usb_quirkstr(quirk));
200				return (1);
201			}
202		}
203		/* no quirk found */
204		break;
205	}
206	mtx_unlock(&usb_quirk_mtx);
207	return (0);
208}
209
210static struct usb_quirk_entry *
211usb_quirk_get_entry(uint16_t vid, uint16_t pid,
212    uint16_t lo_rev, uint16_t hi_rev, uint8_t do_alloc)
213{
214	uint16_t x;
215
216	mtx_assert(&usb_quirk_mtx, MA_OWNED);
217
218	if ((vid | pid | lo_rev | hi_rev) == 0) {
219		/* all zero - special case */
220		return (usb_quirks + USB_DEV_QUIRKS_MAX - 1);
221	}
222	/* search for an existing entry */
223	for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) {
224		/* see if quirk information does not match */
225		if ((usb_quirks[x].vid != vid) ||
226		    (usb_quirks[x].pid != pid) ||
227		    (usb_quirks[x].lo_rev != lo_rev) ||
228		    (usb_quirks[x].hi_rev != hi_rev)) {
229			continue;
230		}
231		return (usb_quirks + x);
232	}
233
234	if (do_alloc == 0) {
235		/* no match */
236		return (NULL);
237	}
238	/* search for a free entry */
239	for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) {
240		/* see if quirk information does not match */
241		if ((usb_quirks[x].vid |
242		    usb_quirks[x].pid |
243		    usb_quirks[x].lo_rev |
244		    usb_quirks[x].hi_rev) != 0) {
245			continue;
246		}
247		usb_quirks[x].vid = vid;
248		usb_quirks[x].pid = pid;
249		usb_quirks[x].lo_rev = lo_rev;
250		usb_quirks[x].hi_rev = hi_rev;
251
252		return (usb_quirks + x);
253	}
254
255	/* no entry found */
256	return (NULL);
257}
258
259/*------------------------------------------------------------------------*
260 *	usb_quirk_ioctl - handle quirk IOCTLs
261 *
262 * Returns:
263 * 0: Success
264 * Else: Failure
265 *------------------------------------------------------------------------*/
266static int
267usb_quirk_ioctl(unsigned long cmd, caddr_t data,
268    int fflag, struct thread *td)
269{
270	struct usb_gen_quirk *pgq;
271	struct usb_quirk_entry *pqe;
272	uint32_t x;
273	uint32_t y;
274	int err;
275
276	switch (cmd) {
277	case USB_DEV_QUIRK_GET:
278		pgq = (void *)data;
279		x = pgq->index % USB_SUB_QUIRKS_MAX;
280		y = pgq->index / USB_SUB_QUIRKS_MAX;
281		if (y >= USB_DEV_QUIRKS_MAX) {
282			return (EINVAL);
283		}
284		mtx_lock(&usb_quirk_mtx);
285		/* copy out data */
286		pgq->vid = usb_quirks[y].vid;
287		pgq->pid = usb_quirks[y].pid;
288		pgq->bcdDeviceLow = usb_quirks[y].lo_rev;
289		pgq->bcdDeviceHigh = usb_quirks[y].hi_rev;
290		strlcpy(pgq->quirkname,
291		    usb_quirkstr(usb_quirks[y].quirks[x]),
292		    sizeof(pgq->quirkname));
293		mtx_unlock(&usb_quirk_mtx);
294		return (0);		/* success */
295
296	case USB_QUIRK_NAME_GET:
297		pgq = (void *)data;
298		x = pgq->index;
299		if (x >= USB_QUIRK_MAX) {
300			return (EINVAL);
301		}
302		strlcpy(pgq->quirkname,
303		    usb_quirkstr(x), sizeof(pgq->quirkname));
304		return (0);		/* success */
305
306	case USB_DEV_QUIRK_ADD:
307		pgq = (void *)data;
308
309		/* check privileges */
310		err = priv_check(curthread, PRIV_DRIVER);
311		if (err) {
312			return (err);
313		}
314		/* convert quirk string into numerical */
315		for (y = 0; y != USB_DEV_QUIRKS_MAX; y++) {
316			if (strcmp(pgq->quirkname, usb_quirkstr(y)) == 0) {
317				break;
318			}
319		}
320		if (y == USB_DEV_QUIRKS_MAX) {
321			return (EINVAL);
322		}
323		if (y == UQ_NONE) {
324			return (EINVAL);
325		}
326		mtx_lock(&usb_quirk_mtx);
327		pqe = usb_quirk_get_entry(pgq->vid, pgq->pid,
328		    pgq->bcdDeviceLow, pgq->bcdDeviceHigh, 1);
329		for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) {
330			if (pqe->quirks[x] == UQ_NONE) {
331				pqe->quirks[x] = y;
332				break;
333			}
334		}
335		mtx_unlock(&usb_quirk_mtx);
336		if (x == USB_SUB_QUIRKS_MAX) {
337			return (ENOMEM);
338		}
339		return (0);		/* success */
340
341	case USB_DEV_QUIRK_REMOVE:
342		pgq = (void *)data;
343		/* check privileges */
344		err = priv_check(curthread, PRIV_DRIVER);
345		if (err) {
346			return (err);
347		}
348		/* convert quirk string into numerical */
349		for (y = 0; y != USB_DEV_QUIRKS_MAX; y++) {
350			if (strcmp(pgq->quirkname, usb_quirkstr(y)) == 0) {
351				break;
352			}
353		}
354		if (y == USB_DEV_QUIRKS_MAX) {
355			return (EINVAL);
356		}
357		if (y == UQ_NONE) {
358			return (EINVAL);
359		}
360		mtx_lock(&usb_quirk_mtx);
361		pqe = usb_quirk_get_entry(pgq->vid, pgq->pid,
362		    pgq->bcdDeviceLow, pgq->bcdDeviceHigh, 0);
363		for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) {
364			if (pqe->quirks[x] == y) {
365				pqe->quirks[x] = UQ_NONE;
366				break;
367			}
368		}
369		if (x == USB_SUB_QUIRKS_MAX) {
370			mtx_unlock(&usb_quirk_mtx);
371			return (ENOMEM);
372		}
373		for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) {
374			if (pqe->quirks[x] != UQ_NONE) {
375				break;
376			}
377		}
378		if (x == USB_SUB_QUIRKS_MAX) {
379			/* all quirk entries are unused - release */
380			memset(pqe, 0, sizeof(pqe));
381		}
382		mtx_unlock(&usb_quirk_mtx);
383		return (0);		/* success */
384
385	default:
386		break;
387	}
388	return (ENOIOCTL);
389}
390
391static void
392usb_quirk_init(void *arg)
393{
394	/* initialize mutex */
395	mtx_init(&usb_quirk_mtx, "USB quirk", NULL, MTX_DEF);
396
397	/* register our function */
398	usb_test_quirk_p = &usb_test_quirk_by_info;
399	usb_quirk_ioctl_p = &usb_quirk_ioctl;
400}
401
402static void
403usb_quirk_uninit(void *arg)
404{
405	usb_quirk_unload(arg);
406
407	/* destroy mutex */
408	mtx_destroy(&usb_quirk_mtx);
409}
410
411SYSINIT(usb_quirk_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_quirk_init, NULL);
412SYSUNINIT(usb_quirk_uninit, SI_SUB_LOCK, SI_ORDER_ANY, usb_quirk_uninit, NULL);
413