udsbr.c revision 1.13
11.13Sdyoung/*	$NetBSD: udsbr.c,v 1.13 2008/02/18 05:31:24 dyoung Exp $	*/
21.1Saugustss
31.1Saugustss/*
41.1Saugustss * Copyright (c) 2002 The NetBSD Foundation, Inc.
51.1Saugustss * All rights reserved.
61.1Saugustss *
71.1Saugustss * This code is derived from software contributed to The NetBSD Foundation
81.3Saugustss * by Lennart Augustsson (lennart@augustsson.net).
91.1Saugustss *
101.1Saugustss * Redistribution and use in source and binary forms, with or without
111.1Saugustss * modification, are permitted provided that the following conditions
121.1Saugustss * are met:
131.1Saugustss * 1. Redistributions of source code must retain the above copyright
141.1Saugustss *    notice, this list of conditions and the following disclaimer.
151.1Saugustss * 2. Redistributions in binary form must reproduce the above copyright
161.1Saugustss *    notice, this list of conditions and the following disclaimer in the
171.1Saugustss *    documentation and/or other materials provided with the distribution.
181.1Saugustss * 3. All advertising materials mentioning features or use of this software
191.1Saugustss *    must display the following acknowledgement:
201.1Saugustss *        This product includes software developed by the NetBSD
211.1Saugustss *        Foundation, Inc. and its contributors.
221.1Saugustss * 4. Neither the name of The NetBSD Foundation nor the names of its
231.1Saugustss *    contributors may be used to endorse or promote products derived
241.1Saugustss *    from this software without specific prior written permission.
251.1Saugustss *
261.1Saugustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
271.1Saugustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
281.1Saugustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
291.1Saugustss * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
301.1Saugustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
311.1Saugustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
321.1Saugustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
331.1Saugustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
341.1Saugustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
351.1Saugustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
361.1Saugustss * POSSIBILITY OF SUCH DAMAGE.
371.1Saugustss */
381.1Saugustss
391.2Saugustss/*
401.2Saugustss * Driver for the D-Link DSB-R100 FM radio.
411.3Saugustss * I apologize for the magic hex constants, but this is what happens
421.2Saugustss * when you have to reverse engineer the driver.
431.2Saugustss * Parts of the code borrowed from Linux and parts from Warner Losh's
441.2Saugustss * FreeBSD driver.
451.2Saugustss */
461.2Saugustss
471.1Saugustss#include <sys/cdefs.h>
481.13Sdyoung__KERNEL_RCSID(0, "$NetBSD: udsbr.c,v 1.13 2008/02/18 05:31:24 dyoung Exp $");
491.1Saugustss
501.1Saugustss#include <sys/param.h>
511.1Saugustss#include <sys/systm.h>
521.1Saugustss#include <sys/kernel.h>
531.1Saugustss#include <sys/device.h>
541.1Saugustss
551.1Saugustss#include <sys/radioio.h>
561.1Saugustss#include <dev/radio_if.h>
571.1Saugustss
581.1Saugustss#include <dev/usb/usb.h>
591.1Saugustss#include <dev/usb/usbdi.h>
601.1Saugustss#include <dev/usb/usbdi_util.h>
611.1Saugustss
621.1Saugustss#include <dev/usb/usbdevs.h>
631.1Saugustss
641.1Saugustss#ifdef UDSBR_DEBUG
651.1Saugustss#define DPRINTF(x)	if (udsbrdebug) logprintf x
661.1Saugustss#define DPRINTFN(n,x)	if (udsbrdebug>(n)) logprintf x
671.1Saugustssint	udsbrdebug = 0;
681.1Saugustss#else
691.1Saugustss#define DPRINTF(x)
701.1Saugustss#define DPRINTFN(n,x)
711.1Saugustss#endif
721.1Saugustss
731.1Saugustss#define UDSBR_CONFIG_NO		1
741.1Saugustss
751.5SaugustssStatic	int     udsbr_get_info(void *, struct radio_info *);
761.5SaugustssStatic	int     udsbr_set_info(void *, struct radio_info *);
771.1Saugustss
781.9Syamtconst struct radio_hw_if udsbr_hw_if = {
791.1Saugustss	NULL, /* open */
801.1Saugustss	NULL, /* close */
811.1Saugustss	udsbr_get_info,
821.1Saugustss	udsbr_set_info,
831.1Saugustss	NULL
841.1Saugustss};
851.1Saugustss
861.1Saugustssstruct udsbr_softc {
871.1Saugustss 	USBBASEDEVICE		sc_dev;
881.1Saugustss	usbd_device_handle	sc_udev;
891.1Saugustss
901.1Saugustss	char			sc_mute;
911.1Saugustss	char			sc_vol;
921.1Saugustss	u_int32_t		sc_freq;
931.1Saugustss
941.1Saugustss	struct device		*sc_child;
951.1Saugustss
961.1Saugustss	char			sc_dying;
971.1Saugustss};
981.1Saugustss
991.5SaugustssStatic	int	udsbr_req(struct udsbr_softc *sc, int ureq, int value,
1001.5Saugustss			  int index);
1011.5SaugustssStatic	void	udsbr_start(struct udsbr_softc *sc);
1021.5SaugustssStatic	void	udsbr_stop(struct udsbr_softc *sc);
1031.5SaugustssStatic	void	udsbr_setfreq(struct udsbr_softc *sc, int freq);
1041.5SaugustssStatic	int	udsbr_status(struct udsbr_softc *sc);
1051.1Saugustss
1061.13Sdyoungint udsbr_match(device_t, struct cfdata *, void *);
1071.13Sdyoungvoid udsbr_attach(device_t, device_t, void *);
1081.13Sdyoungvoid udsbr_childdet(device_t, device_t);
1091.13Sdyoungint udsbr_detach(device_t, int);
1101.13Sdyoungint udsbr_activate(device_t, enum devact);
1111.13Sdyoungextern struct cfdriver udsbr_cd;
1121.13SdyoungCFATTACH_DECL2(udsbr, sizeof(struct udsbr_softc), udsbr_match,
1131.13Sdyoung    udsbr_attach, udsbr_detach, udsbr_activate, NULL, udsbr_childdet);
1141.1Saugustss
1151.1SaugustssUSB_MATCH(udsbr)
1161.1Saugustss{
1171.1Saugustss	USB_MATCH_START(udsbr, uaa);
1181.1Saugustss
1191.1Saugustss	DPRINTFN(50,("udsbr_match\n"));
1201.1Saugustss
1211.1Saugustss	if (uaa->vendor != USB_VENDOR_CYPRESS ||
1221.1Saugustss	    uaa->product != USB_PRODUCT_CYPRESS_FMRADIO)
1231.1Saugustss		return (UMATCH_NONE);
1241.1Saugustss	return (UMATCH_VENDOR_PRODUCT);
1251.1Saugustss}
1261.1Saugustss
1271.1SaugustssUSB_ATTACH(udsbr)
1281.1Saugustss{
1291.1Saugustss	USB_ATTACH_START(udsbr, sc, uaa);
1301.1Saugustss	usbd_device_handle	dev = uaa->device;
1311.10Saugustss	char			*devinfop;
1321.1Saugustss	usbd_status		err;
1331.1Saugustss
1341.1Saugustss	DPRINTFN(10,("udsbr_attach: sc=%p\n", sc));
1351.1Saugustss
1361.10Saugustss	devinfop = usbd_devinfo_alloc(dev, 0);
1371.1Saugustss	USB_ATTACH_SETUP;
1381.10Saugustss	printf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfop);
1391.10Saugustss	usbd_devinfo_free(devinfop);
1401.1Saugustss
1411.1Saugustss	err = usbd_set_config_no(dev, UDSBR_CONFIG_NO, 1);
1421.1Saugustss	if (err) {
1431.1Saugustss		printf("%s: setting config no failed\n",
1441.1Saugustss		    USBDEVNAME(sc->sc_dev));
1451.1Saugustss		USB_ATTACH_ERROR_RETURN;
1461.1Saugustss	}
1471.1Saugustss
1481.1Saugustss	sc->sc_udev = dev;
1491.1Saugustss
1501.1Saugustss	DPRINTFN(10, ("udsbr_attach: %p\n", sc->sc_udev));
1511.1Saugustss
1521.1Saugustss	usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
1531.1Saugustss			   USBDEV(sc->sc_dev));
1541.1Saugustss
1551.1Saugustss	sc->sc_child = radio_attach_mi(&udsbr_hw_if, sc, USBDEV(sc->sc_dev));
1561.1Saugustss
1571.1Saugustss	USB_ATTACH_SUCCESS_RETURN;
1581.1Saugustss}
1591.1Saugustss
1601.13Sdyoungvoid
1611.13Sdyoungudsbr_childdet(device_t self, device_t child)
1621.13Sdyoung{
1631.13Sdyoung}
1641.13Sdyoung
1651.1SaugustssUSB_DETACH(udsbr)
1661.1Saugustss{
1671.1Saugustss	USB_DETACH_START(udsbr, sc);
1681.1Saugustss	int rv = 0;
1691.1Saugustss
1701.1Saugustss	if (sc->sc_child != NULL)
1711.1Saugustss		rv = config_detach(sc->sc_child, flags);
1721.1Saugustss
1731.1Saugustss	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
1741.1Saugustss			   USBDEV(sc->sc_dev));
1751.1Saugustss
1761.1Saugustss	return (rv);
1771.1Saugustss}
1781.1Saugustss
1791.1Saugustssint
1801.1Saugustssudsbr_activate(device_ptr_t self, enum devact act)
1811.1Saugustss{
1821.1Saugustss	struct udsbr_softc *sc = (struct udsbr_softc *)self;
1831.1Saugustss	int rv = 0;
1841.1Saugustss
1851.1Saugustss	switch (act) {
1861.1Saugustss	case DVACT_ACTIVATE:
1871.1Saugustss		return (EOPNOTSUPP);
1881.1Saugustss		break;
1891.1Saugustss
1901.1Saugustss	case DVACT_DEACTIVATE:
1911.1Saugustss		sc->sc_dying = 1;
1921.1Saugustss		if (sc->sc_child != NULL)
1931.1Saugustss			rv = config_deactivate(sc->sc_child);
1941.1Saugustss		break;
1951.1Saugustss	}
1961.1Saugustss	return (rv);
1971.1Saugustss}
1981.1Saugustss
1991.1Saugustssint
2001.1Saugustssudsbr_req(struct udsbr_softc *sc, int ureq, int value, int index)
2011.1Saugustss{
2021.1Saugustss	usb_device_request_t req;
2031.1Saugustss	usbd_status err;
2041.1Saugustss	u_char data;
2051.1Saugustss
2061.4Saugustss	DPRINTFN(1,("udsbr_req: ureq=0x%02x value=0x%04x index=0x%04x\n",
2071.4Saugustss		    ureq, value, index));
2081.1Saugustss	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2091.1Saugustss	req.bRequest = ureq;
2101.1Saugustss	USETW(req.wValue, value);
2111.1Saugustss	USETW(req.wIndex, index);
2121.1Saugustss	USETW(req.wLength, 1);
2131.1Saugustss	err = usbd_do_request(sc->sc_udev, &req, &data);
2141.1Saugustss	if (err) {
2151.1Saugustss		printf("%s: request failed err=%d\n", USBDEVNAME(sc->sc_dev),
2161.1Saugustss		       err);
2171.1Saugustss	}
2181.1Saugustss	return !(data & 1);
2191.1Saugustss}
2201.1Saugustss
2211.1Saugustssvoid
2221.1Saugustssudsbr_start(struct udsbr_softc *sc)
2231.1Saugustss{
2241.1Saugustss	(void)udsbr_req(sc, 0x00, 0x0000, 0x00c7);
2251.1Saugustss	(void)udsbr_req(sc, 0x02, 0x0001, 0x0000);
2261.1Saugustss}
2271.1Saugustss
2281.1Saugustssvoid
2291.1Saugustssudsbr_stop(struct udsbr_softc *sc)
2301.1Saugustss{
2311.1Saugustss	(void)udsbr_req(sc, 0x00, 0x0016, 0x001c);
2321.1Saugustss	(void)udsbr_req(sc, 0x02, 0x0000, 0x0000);
2331.1Saugustss}
2341.1Saugustss
2351.1Saugustssvoid
2361.1Saugustssudsbr_setfreq(struct udsbr_softc *sc, int freq)
2371.1Saugustss{
2381.1Saugustss	DPRINTF(("udsbr_setfreq: setfreq=%d\n", freq));
2391.2Saugustss        /*
2401.2Saugustss         * Freq now is in Hz.  We need to convert it to the frequency
2411.2Saugustss         * that the radio wants.  This frequency is 10.7MHz above
2421.2Saugustss         * the actual frequency.  We then need to convert to
2431.2Saugustss         * units of 12.5kHz.  We add one to the IFM to make rounding
2441.7Saugustss         * easier.
2451.2Saugustss         */
2461.4Saugustss        freq = (freq * 1000 + 10700001) / 12500;
2471.1Saugustss	(void)udsbr_req(sc, 0x01, (freq >> 8) & 0xff, freq & 0xff);
2481.1Saugustss	(void)udsbr_req(sc, 0x00, 0x0096, 0x00b7);
2491.4Saugustss	usbd_delay_ms(sc->sc_udev, 240); /* wait for signal to settle */
2501.1Saugustss}
2511.1Saugustss
2521.1Saugustssint
2531.1Saugustssudsbr_status(struct udsbr_softc *sc)
2541.1Saugustss{
2551.1Saugustss	return (udsbr_req(sc, 0x00, 0x0000, 0x0024));
2561.1Saugustss}
2571.1Saugustss
2581.1Saugustss
2591.1Saugustssint
2601.1Saugustssudsbr_get_info(void *v, struct radio_info *ri)
2611.1Saugustss{
2621.1Saugustss	struct udsbr_softc *sc = v;
2631.1Saugustss
2641.1Saugustss	ri->mute = sc->sc_mute;
2651.1Saugustss	ri->volume = sc->sc_vol ? 255 : 0;
2661.1Saugustss	ri->caps = RADIO_CAPS_DETECT_STEREO;
2671.1Saugustss	ri->rfreq = 0;
2681.1Saugustss	ri->lock = 0;
2691.1Saugustss	ri->freq = sc->sc_freq;
2701.1Saugustss	ri->info = udsbr_status(sc) ? RADIO_INFO_STEREO : 0;
2711.1Saugustss
2721.1Saugustss	return (0);
2731.1Saugustss}
2741.1Saugustss
2751.1Saugustssint
2761.1Saugustssudsbr_set_info(void *v, struct radio_info *ri)
2771.1Saugustss{
2781.1Saugustss	struct udsbr_softc *sc = v;
2791.1Saugustss
2801.1Saugustss	sc->sc_mute = ri->mute != 0;
2811.1Saugustss	sc->sc_vol = ri->volume != 0;
2821.1Saugustss	sc->sc_freq = ri->freq;
2831.1Saugustss	udsbr_setfreq(sc, sc->sc_freq);
2841.1Saugustss
2851.1Saugustss	if (sc->sc_mute || sc->sc_vol == 0)
2861.1Saugustss		udsbr_stop(sc);
2871.1Saugustss	else
2881.1Saugustss		udsbr_start(sc);
2891.1Saugustss
2901.1Saugustss	return (0);
2911.1Saugustss}
292