xref: /netbsd-src/sys/dev/usb/auvitek_audio.c (revision c38e7cc395b1472a774ff828e46123de44c628e9)
1 /* $NetBSD: auvitek_audio.c,v 1.3 2017/06/01 02:45:11 chs Exp $ */
2 
3 /*-
4  * Copyright (c) 2010 Jared D. McNeill <jmcneill@invisible.ca>
5  * 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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * Auvitek AU0828 USB controller - audio support
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: auvitek_audio.c,v 1.3 2017/06/01 02:45:11 chs Exp $");
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/conf.h>
40 #include <sys/bus.h>
41 #include <sys/kmem.h>
42 
43 #include <dev/usb/usb.h>
44 #include <dev/usb/usbdi.h>
45 #include <dev/usb/usbdivar.h>
46 #include <dev/usb/usbdi_util.h>
47 #include <dev/usb/usbdevs.h>
48 
49 #include <dev/usb/auvitekreg.h>
50 #include <dev/usb/auvitekvar.h>
51 
52 #include "locators.h"
53 
54 static int	auvitek_ifprint(void *, const char *);
55 
56 int
57 auvitek_audio_attach(struct auvitek_softc *sc)
58 {
59 	struct usbif_attach_arg uiaa;
60 	struct usbd_device *udev = sc->sc_udev;
61 	usb_device_descriptor_t *dd = &udev->ud_ddesc;
62 	struct usbd_interface **ifaces;
63 	int ilocs[USBIFIFCF_NLOCS], nifaces, i;
64 
65 	nifaces = udev->ud_cdesc->bNumInterface;
66 	ifaces = kmem_zalloc(nifaces * sizeof(*ifaces), KM_SLEEP);
67 	for (i = 0; i < nifaces; i++) {
68 		ifaces[i] = &udev->ud_ifaces[i];
69 	}
70 
71 	uiaa.uiaa_device = udev;
72 	uiaa.uiaa_port = sc->sc_uport;
73 	uiaa.uiaa_vendor = UGETW(dd->idVendor);
74 	uiaa.uiaa_product = UGETW(dd->idProduct);
75 	uiaa.uiaa_release = UGETW(dd->bcdDevice);
76 	uiaa.uiaa_configno = udev->ud_cdesc->bConfigurationValue;
77 	uiaa.uiaa_ifaces = ifaces;
78 	uiaa.uiaa_nifaces = nifaces;
79 	ilocs[USBIFIFCF_PORT] = uiaa.uiaa_port;
80 	ilocs[USBIFIFCF_VENDOR] = uiaa.uiaa_vendor;
81 	ilocs[USBIFIFCF_PRODUCT] = uiaa.uiaa_product;
82 	ilocs[USBIFIFCF_RELEASE] = uiaa.uiaa_release;
83 	ilocs[USBIFIFCF_CONFIGURATION] = uiaa.uiaa_configno;
84 
85 	for (i = 0; i < nifaces; i++) {
86 		if (!ifaces[i])
87 			continue;
88 		uiaa.uiaa_class = ifaces[i]->ui_idesc->bInterfaceClass;
89 		uiaa.uiaa_subclass = ifaces[i]->ui_idesc->bInterfaceSubClass;
90 		if (uiaa.uiaa_class != UICLASS_AUDIO)
91 			continue;
92 		if (uiaa.uiaa_subclass != UISUBCLASS_AUDIOCONTROL)
93 			continue;
94 		uiaa.uiaa_iface = ifaces[i];
95 		uiaa.uiaa_proto = ifaces[i]->ui_idesc->bInterfaceProtocol;
96 		uiaa.uiaa_ifaceno = ifaces[i]->ui_idesc->bInterfaceNumber;
97 		ilocs[USBIFIFCF_INTERFACE] = uiaa.uiaa_ifaceno;
98 		sc->sc_audiodev = config_found_sm_loc(sc->sc_dev, "usbifif",
99 		    ilocs, &uiaa, auvitek_ifprint, config_stdsubmatch);
100 		if (sc->sc_audiodev)
101 			break;
102 	}
103 
104 	kmem_free(ifaces, nifaces * sizeof(*ifaces));
105 
106 	return 0;
107 }
108 
109 int
110 auvitek_audio_detach(struct auvitek_softc *sc, int flags)
111 {
112 	if (sc->sc_audiodev != NULL) {
113 		config_detach(sc->sc_audiodev, flags);
114 		sc->sc_audiodev = NULL;
115 	}
116 
117 	return 0;
118 }
119 
120 void
121 auvitek_audio_childdet(struct auvitek_softc *sc, device_t child)
122 {
123 	if (sc->sc_audiodev == child)
124 		sc->sc_audiodev = NULL;
125 }
126 
127 static int
128 auvitek_ifprint(void *opaque, const char *pnp)
129 {
130 	struct usbif_attach_arg *uiaa = opaque;
131 
132 	if (pnp)
133 		return QUIET;
134 
135 	aprint_normal(" port %d", uiaa->uiaa_port);
136 	aprint_normal(" configuration %d", uiaa->uiaa_configno);
137 	aprint_normal(" interface %d", uiaa->uiaa_ifaceno);
138 
139 	return UNCONF;
140 }
141