1 /* $NetBSD: auvitek_audio.c,v 1.2 2016/04/23 10:15:31 skrll 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.2 2016/04/23 10:15:31 skrll 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
auvitek_audio_attach(struct auvitek_softc * sc)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 	if (ifaces == NULL) {
68 		aprint_error_dev(sc->sc_dev, "audio attach: no memory\n");
69 		return ENOMEM;
70 	}
71 	for (i = 0; i < nifaces; i++) {
72 		ifaces[i] = &udev->ud_ifaces[i];
73 	}
74 
75 	uiaa.uiaa_device = udev;
76 	uiaa.uiaa_port = sc->sc_uport;
77 	uiaa.uiaa_vendor = UGETW(dd->idVendor);
78 	uiaa.uiaa_product = UGETW(dd->idProduct);
79 	uiaa.uiaa_release = UGETW(dd->bcdDevice);
80 	uiaa.uiaa_configno = udev->ud_cdesc->bConfigurationValue;
81 	uiaa.uiaa_ifaces = ifaces;
82 	uiaa.uiaa_nifaces = nifaces;
83 	ilocs[USBIFIFCF_PORT] = uiaa.uiaa_port;
84 	ilocs[USBIFIFCF_VENDOR] = uiaa.uiaa_vendor;
85 	ilocs[USBIFIFCF_PRODUCT] = uiaa.uiaa_product;
86 	ilocs[USBIFIFCF_RELEASE] = uiaa.uiaa_release;
87 	ilocs[USBIFIFCF_CONFIGURATION] = uiaa.uiaa_configno;
88 
89 	for (i = 0; i < nifaces; i++) {
90 		if (!ifaces[i])
91 			continue;
92 		uiaa.uiaa_class = ifaces[i]->ui_idesc->bInterfaceClass;
93 		uiaa.uiaa_subclass = ifaces[i]->ui_idesc->bInterfaceSubClass;
94 		if (uiaa.uiaa_class != UICLASS_AUDIO)
95 			continue;
96 		if (uiaa.uiaa_subclass != UISUBCLASS_AUDIOCONTROL)
97 			continue;
98 		uiaa.uiaa_iface = ifaces[i];
99 		uiaa.uiaa_proto = ifaces[i]->ui_idesc->bInterfaceProtocol;
100 		uiaa.uiaa_ifaceno = ifaces[i]->ui_idesc->bInterfaceNumber;
101 		ilocs[USBIFIFCF_INTERFACE] = uiaa.uiaa_ifaceno;
102 		sc->sc_audiodev = config_found_sm_loc(sc->sc_dev, "usbifif",
103 		    ilocs, &uiaa, auvitek_ifprint, config_stdsubmatch);
104 		if (sc->sc_audiodev)
105 			break;
106 	}
107 
108 	kmem_free(ifaces, nifaces * sizeof(*ifaces));
109 
110 	return 0;
111 }
112 
113 int
auvitek_audio_detach(struct auvitek_softc * sc,int flags)114 auvitek_audio_detach(struct auvitek_softc *sc, int flags)
115 {
116 	if (sc->sc_audiodev != NULL) {
117 		config_detach(sc->sc_audiodev, flags);
118 		sc->sc_audiodev = NULL;
119 	}
120 
121 	return 0;
122 }
123 
124 void
auvitek_audio_childdet(struct auvitek_softc * sc,device_t child)125 auvitek_audio_childdet(struct auvitek_softc *sc, device_t child)
126 {
127 	if (sc->sc_audiodev == child)
128 		sc->sc_audiodev = NULL;
129 }
130 
131 static int
auvitek_ifprint(void * opaque,const char * pnp)132 auvitek_ifprint(void *opaque, const char *pnp)
133 {
134 	struct usbif_attach_arg *uiaa = opaque;
135 
136 	if (pnp)
137 		return QUIET;
138 
139 	aprint_normal(" port %d", uiaa->uiaa_port);
140 	aprint_normal(" configuration %d", uiaa->uiaa_configno);
141 	aprint_normal(" interface %d", uiaa->uiaa_ifaceno);
142 
143 	return UNCONF;
144 }
145