Home | History | Annotate | Line # | Download | only in usb
auvitek_audio.c revision 1.4
      1 /* $NetBSD: auvitek_audio.c,v 1.4 2021/04/24 23:36:59 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 Jared D. McNeill <jmcneill (at) 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.4 2021/04/24 23:36:59 thorpej 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 =
     99 		    config_found(sc->sc_dev, &uiaa, auvitek_ifprint,
    100 				 CFARG_SUBMATCH, config_stdsubmatch,
    101 				 CFARG_IATTR, "usbifif",
    102 				 CFARG_LOCATORS, ilocs,
    103 				 CFARG_EOL);
    104 		if (sc->sc_audiodev)
    105 			break;
    106 	}
    107 
    108 	kmem_free(ifaces, nifaces * sizeof(*ifaces));
    109 
    110 	return 0;
    111 }
    112 
    113 int
    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
    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
    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