udsbr.c revision 1.21
11.21Smrg/*	$NetBSD: udsbr.c,v 1.21 2012/03/11 01:06:07 mrg 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 *
191.1Saugustss * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Saugustss * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Saugustss * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Saugustss * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Saugustss * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Saugustss * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Saugustss * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Saugustss * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Saugustss * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Saugustss * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Saugustss * POSSIBILITY OF SUCH DAMAGE.
301.1Saugustss */
311.1Saugustss
321.2Saugustss/*
331.2Saugustss * Driver for the D-Link DSB-R100 FM radio.
341.3Saugustss * I apologize for the magic hex constants, but this is what happens
351.2Saugustss * when you have to reverse engineer the driver.
361.2Saugustss * Parts of the code borrowed from Linux and parts from Warner Losh's
371.2Saugustss * FreeBSD driver.
381.2Saugustss */
391.2Saugustss
401.1Saugustss#include <sys/cdefs.h>
411.21Smrg__KERNEL_RCSID(0, "$NetBSD: udsbr.c,v 1.21 2012/03/11 01:06:07 mrg Exp $");
421.1Saugustss
431.1Saugustss#include <sys/param.h>
441.1Saugustss#include <sys/systm.h>
451.1Saugustss#include <sys/kernel.h>
461.1Saugustss#include <sys/device.h>
471.1Saugustss
481.1Saugustss#include <sys/radioio.h>
491.1Saugustss#include <dev/radio_if.h>
501.1Saugustss
511.1Saugustss#include <dev/usb/usb.h>
521.1Saugustss#include <dev/usb/usbdi.h>
531.1Saugustss#include <dev/usb/usbdi_util.h>
541.21Smrg#include <dev/usb/usbdivar.h>
551.1Saugustss
561.1Saugustss#include <dev/usb/usbdevs.h>
571.1Saugustss
581.1Saugustss#ifdef UDSBR_DEBUG
591.18Sdyoung#define DPRINTF(x)	if (udsbrdebug) printf x
601.18Sdyoung#define DPRINTFN(n,x)	if (udsbrdebug>(n)) printf x
611.1Saugustssint	udsbrdebug = 0;
621.1Saugustss#else
631.1Saugustss#define DPRINTF(x)
641.1Saugustss#define DPRINTFN(n,x)
651.1Saugustss#endif
661.1Saugustss
671.1Saugustss#define UDSBR_CONFIG_NO		1
681.1Saugustss
691.5SaugustssStatic	int     udsbr_get_info(void *, struct radio_info *);
701.5SaugustssStatic	int     udsbr_set_info(void *, struct radio_info *);
711.1Saugustss
721.9Syamtconst struct radio_hw_if udsbr_hw_if = {
731.1Saugustss	NULL, /* open */
741.1Saugustss	NULL, /* close */
751.1Saugustss	udsbr_get_info,
761.1Saugustss	udsbr_set_info,
771.1Saugustss	NULL
781.1Saugustss};
791.1Saugustss
801.1Saugustssstruct udsbr_softc {
811.18Sdyoung 	device_t		sc_dev;
821.1Saugustss	usbd_device_handle	sc_udev;
831.1Saugustss
841.1Saugustss	char			sc_mute;
851.1Saugustss	char			sc_vol;
861.1Saugustss	u_int32_t		sc_freq;
871.1Saugustss
881.15Scube	device_t		sc_child;
891.1Saugustss
901.1Saugustss	char			sc_dying;
911.1Saugustss};
921.1Saugustss
931.5SaugustssStatic	int	udsbr_req(struct udsbr_softc *sc, int ureq, int value,
941.5Saugustss			  int index);
951.5SaugustssStatic	void	udsbr_start(struct udsbr_softc *sc);
961.5SaugustssStatic	void	udsbr_stop(struct udsbr_softc *sc);
971.5SaugustssStatic	void	udsbr_setfreq(struct udsbr_softc *sc, int freq);
981.5SaugustssStatic	int	udsbr_status(struct udsbr_softc *sc);
991.1Saugustss
1001.15Scubeint udsbr_match(device_t, cfdata_t, void *);
1011.13Sdyoungvoid udsbr_attach(device_t, device_t, void *);
1021.13Sdyoungvoid udsbr_childdet(device_t, device_t);
1031.13Sdyoungint udsbr_detach(device_t, int);
1041.13Sdyoungint udsbr_activate(device_t, enum devact);
1051.13Sdyoungextern struct cfdriver udsbr_cd;
1061.15ScubeCFATTACH_DECL2_NEW(udsbr, sizeof(struct udsbr_softc), udsbr_match,
1071.13Sdyoung    udsbr_attach, udsbr_detach, udsbr_activate, NULL, udsbr_childdet);
1081.1Saugustss
1091.20Sjakllschint
1101.18Sdyoungudsbr_match(device_t parent, cfdata_t match, void *aux)
1111.1Saugustss{
1121.18Sdyoung	struct usb_attach_arg *uaa = aux;
1131.1Saugustss
1141.1Saugustss	DPRINTFN(50,("udsbr_match\n"));
1151.1Saugustss
1161.1Saugustss	if (uaa->vendor != USB_VENDOR_CYPRESS ||
1171.1Saugustss	    uaa->product != USB_PRODUCT_CYPRESS_FMRADIO)
1181.1Saugustss		return (UMATCH_NONE);
1191.1Saugustss	return (UMATCH_VENDOR_PRODUCT);
1201.1Saugustss}
1211.1Saugustss
1221.20Sjakllschvoid
1231.18Sdyoungudsbr_attach(device_t parent, device_t self, void *aux)
1241.1Saugustss{
1251.18Sdyoung	struct udsbr_softc *sc = device_private(self);
1261.18Sdyoung	struct usb_attach_arg *uaa = aux;
1271.1Saugustss	usbd_device_handle	dev = uaa->device;
1281.10Saugustss	char			*devinfop;
1291.1Saugustss	usbd_status		err;
1301.1Saugustss
1311.1Saugustss	DPRINTFN(10,("udsbr_attach: sc=%p\n", sc));
1321.1Saugustss
1331.15Scube	sc->sc_dev = self;
1341.15Scube
1351.16Splunky	aprint_naive("\n");
1361.16Splunky	aprint_normal("\n");
1371.16Splunky
1381.10Saugustss	devinfop = usbd_devinfo_alloc(dev, 0);
1391.15Scube	aprint_normal_dev(self, "%s\n", devinfop);
1401.10Saugustss	usbd_devinfo_free(devinfop);
1411.1Saugustss
1421.1Saugustss	err = usbd_set_config_no(dev, UDSBR_CONFIG_NO, 1);
1431.1Saugustss	if (err) {
1441.15Scube		aprint_error_dev(self, "setting config no failed\n");
1451.18Sdyoung		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.18Sdyoung			   sc->sc_dev);
1541.1Saugustss
1551.18Sdyoung	sc->sc_child = radio_attach_mi(&udsbr_hw_if, sc, sc->sc_dev);
1561.1Saugustss
1571.18Sdyoung	return;
1581.1Saugustss}
1591.1Saugustss
1601.13Sdyoungvoid
1611.13Sdyoungudsbr_childdet(device_t self, device_t child)
1621.13Sdyoung{
1631.13Sdyoung}
1641.13Sdyoung
1651.20Sjakllschint
1661.18Sdyoungudsbr_detach(device_t self, int flags)
1671.1Saugustss{
1681.18Sdyoung	struct udsbr_softc *sc = device_private(self);
1691.1Saugustss	int rv = 0;
1701.1Saugustss
1711.1Saugustss	if (sc->sc_child != NULL)
1721.1Saugustss		rv = config_detach(sc->sc_child, flags);
1731.1Saugustss
1741.1Saugustss	usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
1751.18Sdyoung			   sc->sc_dev);
1761.1Saugustss
1771.1Saugustss	return (rv);
1781.1Saugustss}
1791.1Saugustss
1801.1Saugustssint
1811.18Sdyoungudsbr_activate(device_t self, enum devact act)
1821.1Saugustss{
1831.15Scube	struct udsbr_softc *sc = device_private(self);
1841.1Saugustss
1851.1Saugustss	switch (act) {
1861.1Saugustss	case DVACT_DEACTIVATE:
1871.1Saugustss		sc->sc_dying = 1;
1881.17Sdyoung		return 0;
1891.17Sdyoung	default:
1901.17Sdyoung		return EOPNOTSUPP;
1911.1Saugustss	}
1921.1Saugustss}
1931.1Saugustss
1941.1Saugustssint
1951.1Saugustssudsbr_req(struct udsbr_softc *sc, int ureq, int value, int index)
1961.1Saugustss{
1971.1Saugustss	usb_device_request_t req;
1981.1Saugustss	usbd_status err;
1991.1Saugustss	u_char data;
2001.1Saugustss
2011.4Saugustss	DPRINTFN(1,("udsbr_req: ureq=0x%02x value=0x%04x index=0x%04x\n",
2021.4Saugustss		    ureq, value, index));
2031.1Saugustss	req.bmRequestType = UT_READ_VENDOR_DEVICE;
2041.1Saugustss	req.bRequest = ureq;
2051.1Saugustss	USETW(req.wValue, value);
2061.1Saugustss	USETW(req.wIndex, index);
2071.1Saugustss	USETW(req.wLength, 1);
2081.1Saugustss	err = usbd_do_request(sc->sc_udev, &req, &data);
2091.1Saugustss	if (err) {
2101.15Scube		aprint_error_dev(sc->sc_dev, "request failed err=%d\n", err);
2111.1Saugustss	}
2121.1Saugustss	return !(data & 1);
2131.1Saugustss}
2141.1Saugustss
2151.1Saugustssvoid
2161.1Saugustssudsbr_start(struct udsbr_softc *sc)
2171.1Saugustss{
2181.1Saugustss	(void)udsbr_req(sc, 0x00, 0x0000, 0x00c7);
2191.1Saugustss	(void)udsbr_req(sc, 0x02, 0x0001, 0x0000);
2201.1Saugustss}
2211.1Saugustss
2221.1Saugustssvoid
2231.1Saugustssudsbr_stop(struct udsbr_softc *sc)
2241.1Saugustss{
2251.1Saugustss	(void)udsbr_req(sc, 0x00, 0x0016, 0x001c);
2261.1Saugustss	(void)udsbr_req(sc, 0x02, 0x0000, 0x0000);
2271.1Saugustss}
2281.1Saugustss
2291.1Saugustssvoid
2301.1Saugustssudsbr_setfreq(struct udsbr_softc *sc, int freq)
2311.1Saugustss{
2321.1Saugustss	DPRINTF(("udsbr_setfreq: setfreq=%d\n", freq));
2331.2Saugustss        /*
2341.2Saugustss         * Freq now is in Hz.  We need to convert it to the frequency
2351.2Saugustss         * that the radio wants.  This frequency is 10.7MHz above
2361.2Saugustss         * the actual frequency.  We then need to convert to
2371.2Saugustss         * units of 12.5kHz.  We add one to the IFM to make rounding
2381.7Saugustss         * easier.
2391.2Saugustss         */
2401.4Saugustss        freq = (freq * 1000 + 10700001) / 12500;
2411.1Saugustss	(void)udsbr_req(sc, 0x01, (freq >> 8) & 0xff, freq & 0xff);
2421.1Saugustss	(void)udsbr_req(sc, 0x00, 0x0096, 0x00b7);
2431.4Saugustss	usbd_delay_ms(sc->sc_udev, 240); /* wait for signal to settle */
2441.1Saugustss}
2451.1Saugustss
2461.1Saugustssint
2471.1Saugustssudsbr_status(struct udsbr_softc *sc)
2481.1Saugustss{
2491.1Saugustss	return (udsbr_req(sc, 0x00, 0x0000, 0x0024));
2501.1Saugustss}
2511.1Saugustss
2521.1Saugustss
2531.1Saugustssint
2541.1Saugustssudsbr_get_info(void *v, struct radio_info *ri)
2551.1Saugustss{
2561.1Saugustss	struct udsbr_softc *sc = v;
2571.1Saugustss
2581.1Saugustss	ri->mute = sc->sc_mute;
2591.1Saugustss	ri->volume = sc->sc_vol ? 255 : 0;
2601.1Saugustss	ri->caps = RADIO_CAPS_DETECT_STEREO;
2611.1Saugustss	ri->rfreq = 0;
2621.1Saugustss	ri->lock = 0;
2631.1Saugustss	ri->freq = sc->sc_freq;
2641.1Saugustss	ri->info = udsbr_status(sc) ? RADIO_INFO_STEREO : 0;
2651.1Saugustss
2661.1Saugustss	return (0);
2671.1Saugustss}
2681.1Saugustss
2691.1Saugustssint
2701.1Saugustssudsbr_set_info(void *v, struct radio_info *ri)
2711.1Saugustss{
2721.1Saugustss	struct udsbr_softc *sc = v;
2731.1Saugustss
2741.1Saugustss	sc->sc_mute = ri->mute != 0;
2751.1Saugustss	sc->sc_vol = ri->volume != 0;
2761.1Saugustss	sc->sc_freq = ri->freq;
2771.1Saugustss	udsbr_setfreq(sc, sc->sc_freq);
2781.1Saugustss
2791.1Saugustss	if (sc->sc_mute || sc->sc_vol == 0)
2801.1Saugustss		udsbr_stop(sc);
2811.1Saugustss	else
2821.1Saugustss		udsbr_start(sc);
2831.1Saugustss
2841.1Saugustss	return (0);
2851.1Saugustss}
286