usb_quirk.c revision 194677
164567Sgshapiro/* $FreeBSD: head/sys/dev/usb/quirk/usb_quirk.c 194677 2009-06-23 02:19:59Z thompsa $ */
264567Sgshapiro/*-
364567Sgshapiro * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
464567Sgshapiro * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
564567Sgshapiro * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
690798Sgshapiro *
764567Sgshapiro * Redistribution and use in source and binary forms, with or without
864567Sgshapiro * modification, are permitted provided that the following conditions
965970Sgshapiro * are met:
1065970Sgshapiro * 1. Redistributions of source code must retain the above copyright
1165970Sgshapiro *    notice, this list of conditions and the following disclaimer.
12201381Sed * 2. Redistributions in binary form must reproduce the above copyright
13201381Sed *    notice, this list of conditions and the following disclaimer in the
1464567Sgshapiro *    documentation and/or other materials provided with the distribution.
1564567Sgshapiro *
1690798Sgshapiro * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1790798Sgshapiro * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1890798Sgshapiro * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1964567Sgshapiro * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20136910Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2164567Sgshapiro * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2290798Sgshapiro * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2390798Sgshapiro * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2490798Sgshapiro * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2564567Sgshapiro * 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
98	/*
99	 * XXX The following quirks should have a more specific revision
100	 * number:
101	 */
102	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_895C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
103	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_880C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
104	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_815C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
105	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_810C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
106	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_830C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
107	{USB_QUIRK_ENTRY(USB_VENDOR_HP, USB_PRODUCT_HP_1220C, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
108	{USB_QUIRK_ENTRY(USB_VENDOR_XEROX, USB_PRODUCT_XEROX_WCM15, 0x0000, 0xFFFF, UQ_BROKEN_BIDIR, UQ_NONE)},
109	/* Devices which should be ignored by uhid */
110	{USB_QUIRK_ENTRY(USB_VENDOR_APC, USB_PRODUCT_APC_UPS, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
111	{USB_QUIRK_ENTRY(USB_VENDOR_BELKIN, USB_PRODUCT_BELKIN_F6C550AVR, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
112	{USB_QUIRK_ENTRY(USB_VENDOR_CYBERPOWER, USB_PRODUCT_CYBERPOWER_1500CAVRLCD, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
113	{USB_QUIRK_ENTRY(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
114	{USB_QUIRK_ENTRY(USB_VENDOR_ITUNERNET, USB_PRODUCT_ITUNERNET_USBLCD2X20, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
115	{USB_QUIRK_ENTRY(USB_VENDOR_ITUNERNET, USB_PRODUCT_ITUNERNET_USBLCD4X20, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
116	{USB_QUIRK_ENTRY(USB_VENDOR_MGE, USB_PRODUCT_MGE_UPS1, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
117	{USB_QUIRK_ENTRY(USB_VENDOR_MGE, USB_PRODUCT_MGE_UPS2, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
118	{USB_QUIRK_ENTRY(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
119	{USB_QUIRK_ENTRY(USB_VENDOR_APPLE, USB_PRODUCT_APPLE_IPHONE_3G, 0x0000, 0xFFFF, UQ_HID_IGNORE, UQ_NONE)},
120	/* Devices which should be ignored by both ukbd and uhid */
121	{USB_QUIRK_ENTRY(USB_VENDOR_CYPRESS, USB_PRODUCT_CYPRESS_WISPY1A, 0x0000, 0xFFFF, UQ_KBD_IGNORE, UQ_HID_IGNORE, UQ_NONE)},
122	{USB_QUIRK_ENTRY(USB_VENDOR_METAGEEK, USB_PRODUCT_METAGEEK_WISPY1B, 0x0000, 0xFFFF, UQ_KBD_IGNORE, UQ_HID_IGNORE, UQ_NONE)},
123	{USB_QUIRK_ENTRY(USB_VENDOR_TENX, USB_PRODUCT_TENX_UAUDIO0, 0x0101, 0x0101, UQ_AUDIO_SWAP_LR, UQ_NONE)},
124	/* MS keyboards do weird things */
125	{USB_QUIRK_ENTRY(USB_VENDOR_MICROSOFT, USB_PRODUCT_MICROSOFT_WLINTELLIMOUSE, 0x0000, 0xFFFF, UQ_MS_LEADING_BYTE, UQ_NONE)},
126	{USB_QUIRK_ENTRY(USB_VENDOR_METAGEEK, USB_PRODUCT_METAGEEK_WISPY24X, 0x0000, 0xFFFF, UQ_KBD_IGNORE, UQ_HID_IGNORE, UQ_NONE)},
127};
128
129static const char *usb_quirk_str[USB_QUIRK_MAX] = {
130	[UQ_NONE]		= "UQ_NONE",
131	[UQ_AUDIO_SWAP_LR]	= "UQ_AUDIO_SWAP_LR",
132	[UQ_AU_INP_ASYNC]	= "UQ_AU_INP_ASYNC",
133	[UQ_AU_NO_FRAC]		= "UQ_AU_NO_FRAC",
134	[UQ_AU_NO_XU]		= "UQ_AU_NO_XU",
135	[UQ_BAD_ADC]		= "UQ_BAD_ADC",
136	[UQ_BAD_AUDIO]		= "UQ_BAD_AUDIO",
137	[UQ_BROKEN_BIDIR]	= "UQ_BROKEN_BIDIR",
138	[UQ_BUS_POWERED]	= "UQ_BUS_POWERED",
139	[UQ_HID_IGNORE]		= "UQ_HID_IGNORE",
140	[UQ_KBD_IGNORE]		= "UQ_KBD_IGNORE",
141	[UQ_MS_BAD_CLASS]	= "UQ_MS_BAD_CLASS",
142	[UQ_MS_LEADING_BYTE]	= "UQ_MS_LEADING_BYTE",
143	[UQ_MS_REVZ]		= "UQ_MS_REVZ",
144	[UQ_NO_STRINGS]		= "UQ_NO_STRINGS",
145	[UQ_OPEN_CLEARSTALL]	= "UQ_OPEN_CLEARSTALL",
146	[UQ_POWER_CLAIM]	= "UQ_POWER_CLAIM",
147	[UQ_SPUR_BUT_UP]	= "UQ_SPUR_BUT_UP",
148	[UQ_SWAP_UNICODE]	= "UQ_SWAP_UNICODE",
149	[UQ_CFG_INDEX_1]	= "UQ_CFG_INDEX_1",
150	[UQ_CFG_INDEX_2]	= "UQ_CFG_INDEX_2",
151	[UQ_CFG_INDEX_3]	= "UQ_CFG_INDEX_3",
152	[UQ_CFG_INDEX_4]	= "UQ_CFG_INDEX_4",
153	[UQ_CFG_INDEX_0]	= "UQ_CFG_INDEX_0",
154};
155
156/*------------------------------------------------------------------------*
157 *	usb_quirkstr
158 *
159 * This function converts an USB quirk code into a string.
160 *------------------------------------------------------------------------*/
161static const char *
162usb_quirkstr(uint16_t quirk)
163{
164	return ((quirk < USB_QUIRK_MAX) ?
165	    usb_quirk_str[quirk] : "USB_QUIRK_UNKNOWN");
166}
167
168/*------------------------------------------------------------------------*
169 *	usb_test_quirk_by_info
170 *
171 * Returns:
172 * 0: Quirk not found
173 * Else: Quirk found
174 *------------------------------------------------------------------------*/
175static uint8_t
176usb_test_quirk_by_info(const struct usbd_lookup_info *info, uint16_t quirk)
177{
178	uint16_t x;
179	uint16_t y;
180
181	if (quirk == UQ_NONE) {
182		return (0);
183	}
184	mtx_lock(&usb_quirk_mtx);
185
186	for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) {
187		/* see if quirk information does not match */
188		if ((usb_quirks[x].vid != info->idVendor) ||
189		    (usb_quirks[x].pid != info->idProduct) ||
190		    (usb_quirks[x].lo_rev > info->bcdDevice) ||
191		    (usb_quirks[x].hi_rev < info->bcdDevice)) {
192			continue;
193		}
194		/* lookup quirk */
195		for (y = 0; y != USB_SUB_QUIRKS_MAX; y++) {
196			if (usb_quirks[x].quirks[y] == quirk) {
197				mtx_unlock(&usb_quirk_mtx);
198				DPRINTF("Found quirk '%s'.\n", usb_quirkstr(quirk));
199				return (1);
200			}
201		}
202		/* no quirk found */
203		break;
204	}
205	mtx_unlock(&usb_quirk_mtx);
206	return (0);
207}
208
209static struct usb_quirk_entry *
210usb_quirk_get_entry(uint16_t vid, uint16_t pid,
211    uint16_t lo_rev, uint16_t hi_rev, uint8_t do_alloc)
212{
213	uint16_t x;
214
215	mtx_assert(&usb_quirk_mtx, MA_OWNED);
216
217	if ((vid | pid | lo_rev | hi_rev) == 0) {
218		/* all zero - special case */
219		return (usb_quirks + USB_DEV_QUIRKS_MAX - 1);
220	}
221	/* search for an existing entry */
222	for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) {
223		/* see if quirk information does not match */
224		if ((usb_quirks[x].vid != vid) ||
225		    (usb_quirks[x].pid != pid) ||
226		    (usb_quirks[x].lo_rev != lo_rev) ||
227		    (usb_quirks[x].hi_rev != hi_rev)) {
228			continue;
229		}
230		return (usb_quirks + x);
231	}
232
233	if (do_alloc == 0) {
234		/* no match */
235		return (NULL);
236	}
237	/* search for a free entry */
238	for (x = 0; x != USB_DEV_QUIRKS_MAX; x++) {
239		/* see if quirk information does not match */
240		if ((usb_quirks[x].vid |
241		    usb_quirks[x].pid |
242		    usb_quirks[x].lo_rev |
243		    usb_quirks[x].hi_rev) != 0) {
244			continue;
245		}
246		usb_quirks[x].vid = vid;
247		usb_quirks[x].pid = pid;
248		usb_quirks[x].lo_rev = lo_rev;
249		usb_quirks[x].hi_rev = hi_rev;
250
251		return (usb_quirks + x);
252	}
253
254	/* no entry found */
255	return (NULL);
256}
257
258/*------------------------------------------------------------------------*
259 *	usb_quirk_ioctl - handle quirk IOCTLs
260 *
261 * Returns:
262 * 0: Success
263 * Else: Failure
264 *------------------------------------------------------------------------*/
265static int
266usb_quirk_ioctl(unsigned long cmd, caddr_t data,
267    int fflag, struct thread *td)
268{
269	struct usb_gen_quirk *pgq;
270	struct usb_quirk_entry *pqe;
271	uint32_t x;
272	uint32_t y;
273	int err;
274
275	switch (cmd) {
276	case USB_DEV_QUIRK_GET:
277		pgq = (void *)data;
278		x = pgq->index % USB_SUB_QUIRKS_MAX;
279		y = pgq->index / USB_SUB_QUIRKS_MAX;
280		if (y >= USB_DEV_QUIRKS_MAX) {
281			return (EINVAL);
282		}
283		mtx_lock(&usb_quirk_mtx);
284		/* copy out data */
285		pgq->vid = usb_quirks[y].vid;
286		pgq->pid = usb_quirks[y].pid;
287		pgq->bcdDeviceLow = usb_quirks[y].lo_rev;
288		pgq->bcdDeviceHigh = usb_quirks[y].hi_rev;
289		strlcpy(pgq->quirkname,
290		    usb_quirkstr(usb_quirks[y].quirks[x]),
291		    sizeof(pgq->quirkname));
292		mtx_unlock(&usb_quirk_mtx);
293		return (0);		/* success */
294
295	case USB_QUIRK_NAME_GET:
296		pgq = (void *)data;
297		x = pgq->index;
298		if (x >= USB_QUIRK_MAX) {
299			return (EINVAL);
300		}
301		strlcpy(pgq->quirkname,
302		    usb_quirkstr(x), sizeof(pgq->quirkname));
303		return (0);		/* success */
304
305	case USB_DEV_QUIRK_ADD:
306		pgq = (void *)data;
307
308		/* check privileges */
309		err = priv_check(curthread, PRIV_DRIVER);
310		if (err) {
311			return (err);
312		}
313		/* convert quirk string into numerical */
314		for (y = 0; y != USB_DEV_QUIRKS_MAX; y++) {
315			if (strcmp(pgq->quirkname, usb_quirkstr(y)) == 0) {
316				break;
317			}
318		}
319		if (y == USB_DEV_QUIRKS_MAX) {
320			return (EINVAL);
321		}
322		if (y == UQ_NONE) {
323			return (EINVAL);
324		}
325		mtx_lock(&usb_quirk_mtx);
326		pqe = usb_quirk_get_entry(pgq->vid, pgq->pid,
327		    pgq->bcdDeviceLow, pgq->bcdDeviceHigh, 1);
328		for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) {
329			if (pqe->quirks[x] == UQ_NONE) {
330				pqe->quirks[x] = y;
331				break;
332			}
333		}
334		mtx_unlock(&usb_quirk_mtx);
335		if (x == USB_SUB_QUIRKS_MAX) {
336			return (ENOMEM);
337		}
338		return (0);		/* success */
339
340	case USB_DEV_QUIRK_REMOVE:
341		pgq = (void *)data;
342		/* check privileges */
343		err = priv_check(curthread, PRIV_DRIVER);
344		if (err) {
345			return (err);
346		}
347		/* convert quirk string into numerical */
348		for (y = 0; y != USB_DEV_QUIRKS_MAX; y++) {
349			if (strcmp(pgq->quirkname, usb_quirkstr(y)) == 0) {
350				break;
351			}
352		}
353		if (y == USB_DEV_QUIRKS_MAX) {
354			return (EINVAL);
355		}
356		if (y == UQ_NONE) {
357			return (EINVAL);
358		}
359		mtx_lock(&usb_quirk_mtx);
360		pqe = usb_quirk_get_entry(pgq->vid, pgq->pid,
361		    pgq->bcdDeviceLow, pgq->bcdDeviceHigh, 0);
362		for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) {
363			if (pqe->quirks[x] == y) {
364				pqe->quirks[x] = UQ_NONE;
365				break;
366			}
367		}
368		if (x == USB_SUB_QUIRKS_MAX) {
369			mtx_unlock(&usb_quirk_mtx);
370			return (ENOMEM);
371		}
372		for (x = 0; x != USB_SUB_QUIRKS_MAX; x++) {
373			if (pqe->quirks[x] != UQ_NONE) {
374				break;
375			}
376		}
377		if (x == USB_SUB_QUIRKS_MAX) {
378			/* all quirk entries are unused - release */
379			memset(pqe, 0, sizeof(pqe));
380		}
381		mtx_unlock(&usb_quirk_mtx);
382		return (0);		/* success */
383
384	default:
385		break;
386	}
387	return (ENOIOCTL);
388}
389
390static void
391usb_quirk_init(void *arg)
392{
393	/* initialize mutex */
394	mtx_init(&usb_quirk_mtx, "USB quirk", NULL, MTX_DEF);
395
396	/* register our function */
397	usb_test_quirk_p = &usb_test_quirk_by_info;
398	usb_quirk_ioctl_p = &usb_quirk_ioctl;
399}
400
401static void
402usb_quirk_uninit(void *arg)
403{
404	usb_quirk_unload(arg);
405
406	/* destroy mutex */
407	mtx_destroy(&usb_quirk_mtx);
408}
409
410SYSINIT(usb_quirk_init, SI_SUB_LOCK, SI_ORDER_FIRST, usb_quirk_init, NULL);
411SYSUNINIT(usb_quirk_uninit, SI_SUB_LOCK, SI_ORDER_ANY, usb_quirk_uninit, NULL);
412