1 /*        $NetBSD: udsbr.c,v 1.32 2023/05/10 00:12:36 riastradh Exp $ */
2 
3 /*
4  * Copyright (c) 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Lennart Augustsson (lennart@augustsson.net).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Driver for the D-Link DSB-R100 FM radio.
34  * I apologize for the magic hex constants, but this is what happens
35  * when you have to reverse engineer the driver.
36  * Parts of the code borrowed from Linux and parts from Warner Losh's
37  * FreeBSD driver.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: udsbr.c,v 1.32 2023/05/10 00:12:36 riastradh Exp $");
42 
43 #ifdef _KERNEL_OPT
44 #include "opt_usb.h"
45 #endif
46 
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/kernel.h>
50 #include <sys/device.h>
51 
52 #include <sys/radioio.h>
53 #include <dev/radio_if.h>
54 
55 #include <dev/usb/usb.h>
56 #include <dev/usb/usbdi.h>
57 #include <dev/usb/usbdi_util.h>
58 #include <dev/usb/usbdivar.h>
59 
60 #include <dev/usb/usbdevs.h>
61 
62 #ifdef UDSBR_DEBUG
63 #define DPRINTF(x)  if (udsbrdebug) printf x
64 #define DPRINTFN(n,x)         if (udsbrdebug>(n)) printf x
65 int       udsbrdebug = 0;
66 #else
67 #define DPRINTF(x)
68 #define DPRINTFN(n,x)
69 #endif
70 
71 #define UDSBR_CONFIG_NO                 1
72 
73 Static    int     udsbr_get_info(void *, struct radio_info *);
74 Static    int     udsbr_set_info(void *, struct radio_info *);
75 
76 static const struct radio_hw_if udsbr_hw_if = {
77           NULL, /* open */
78           NULL, /* close */
79           udsbr_get_info,
80           udsbr_set_info,
81           NULL
82 };
83 
84 struct udsbr_softc {
85           device_t            sc_dev;
86           struct usbd_device *          sc_udev;
87 
88           char                          sc_mute;
89           char                          sc_vol;
90           uint32_t            sc_freq;
91 
92           device_t            sc_child;
93 
94           char                          sc_dying;
95 };
96 
97 Static    int       udsbr_req(struct udsbr_softc *, int, int,
98                                 int);
99 Static    void      udsbr_start(struct udsbr_softc *);
100 Static    void      udsbr_stop(struct udsbr_softc *);
101 Static    void      udsbr_setfreq(struct udsbr_softc *, int);
102 Static    int       udsbr_status(struct udsbr_softc *);
103 
104 static int udsbr_match(device_t, cfdata_t, void *);
105 static void udsbr_attach(device_t, device_t, void *);
106 static void udsbr_childdet(device_t, device_t);
107 static int udsbr_detach(device_t, int);
108 static int udsbr_activate(device_t, enum devact);
109 
110 CFATTACH_DECL2_NEW(udsbr, sizeof(struct udsbr_softc), udsbr_match,
111     udsbr_attach, udsbr_detach, udsbr_activate, NULL, udsbr_childdet);
112 
113 static int
udsbr_match(device_t parent,cfdata_t match,void * aux)114 udsbr_match(device_t parent, cfdata_t match, void *aux)
115 {
116           struct usb_attach_arg *uaa = aux;
117 
118           DPRINTFN(50,("udsbr_match\n"));
119 
120           if (uaa->uaa_vendor != USB_VENDOR_CYPRESS ||
121               uaa->uaa_product != USB_PRODUCT_CYPRESS_FMRADIO)
122                     return UMATCH_NONE;
123           return UMATCH_VENDOR_PRODUCT;
124 }
125 
126 static void
udsbr_attach(device_t parent,device_t self,void * aux)127 udsbr_attach(device_t parent, device_t self, void *aux)
128 {
129           struct udsbr_softc *sc = device_private(self);
130           struct usb_attach_arg *uaa = aux;
131           struct usbd_device *          dev = uaa->uaa_device;
132           char                          *devinfop;
133           usbd_status                   err;
134 
135           DPRINTFN(10,("udsbr_attach: sc=%p\n", sc));
136 
137           sc->sc_dev = self;
138 
139           aprint_naive("\n");
140           aprint_normal("\n");
141 
142           devinfop = usbd_devinfo_alloc(dev, 0);
143           aprint_normal_dev(self, "%s\n", devinfop);
144           usbd_devinfo_free(devinfop);
145 
146           err = usbd_set_config_no(dev, UDSBR_CONFIG_NO, 1);
147           if (err) {
148                     aprint_error_dev(self, "failed to set configuration"
149                         ", err=%s\n", usbd_errstr(err));
150                     return;
151           }
152 
153           sc->sc_udev = dev;
154 
155           DPRINTFN(10, ("udsbr_attach: %p\n", sc->sc_udev));
156 
157           usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev, sc->sc_dev);
158 
159           sc->sc_child = radio_attach_mi(&udsbr_hw_if, sc, sc->sc_dev);
160 
161           return;
162 }
163 
164 static void
udsbr_childdet(device_t self,device_t child)165 udsbr_childdet(device_t self, device_t child)
166 {
167 }
168 
169 static int
udsbr_detach(device_t self,int flags)170 udsbr_detach(device_t self, int flags)
171 {
172           struct udsbr_softc *sc = device_private(self);
173           int error;
174 
175           error = config_detach_children(self, flags);
176           if (error)
177                     return error;
178 
179           if (sc->sc_udev != NULL)
180                     usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
181                         sc->sc_dev);
182 
183           return 0;
184 }
185 
186 static int
udsbr_activate(device_t self,enum devact act)187 udsbr_activate(device_t self, enum devact act)
188 {
189           struct udsbr_softc *sc = device_private(self);
190 
191           switch (act) {
192           case DVACT_DEACTIVATE:
193                     sc->sc_dying = 1;
194                     return 0;
195           default:
196                     return EOPNOTSUPP;
197           }
198 }
199 
200 int
udsbr_req(struct udsbr_softc * sc,int ureq,int value,int index)201 udsbr_req(struct udsbr_softc *sc, int ureq, int value, int index)
202 {
203           usb_device_request_t req;
204           usbd_status err;
205           u_char data;
206 
207           DPRINTFN(1,("udsbr_req: ureq=0x%02x value=0x%04x index=0x%04x\n",
208                         ureq, value, index));
209           req.bmRequestType = UT_READ_VENDOR_DEVICE;
210           req.bRequest = ureq;
211           USETW(req.wValue, value);
212           USETW(req.wIndex, index);
213           USETW(req.wLength, 1);
214           err = usbd_do_request(sc->sc_udev, &req, &data);
215           if (err) {
216                     aprint_error_dev(sc->sc_dev, "request failed err=%d\n", err);
217           }
218           return !(data & 1);
219 }
220 
221 void
udsbr_start(struct udsbr_softc * sc)222 udsbr_start(struct udsbr_softc *sc)
223 {
224           (void)udsbr_req(sc, 0x00, 0x0000, 0x00c7);
225           (void)udsbr_req(sc, 0x02, 0x0001, 0x0000);
226 }
227 
228 void
udsbr_stop(struct udsbr_softc * sc)229 udsbr_stop(struct udsbr_softc *sc)
230 {
231           (void)udsbr_req(sc, 0x00, 0x0016, 0x001c);
232           (void)udsbr_req(sc, 0x02, 0x0000, 0x0000);
233 }
234 
235 void
udsbr_setfreq(struct udsbr_softc * sc,int freq)236 udsbr_setfreq(struct udsbr_softc *sc, int freq)
237 {
238           DPRINTF(("udsbr_setfreq: setfreq=%d\n", freq));
239           /*
240            * Freq now is in Hz.  We need to convert it to the frequency
241            * that the radio wants.  This frequency is 10.7MHz above
242            * the actual frequency.  We then need to convert to
243            * units of 12.5kHz.  We add one to the IFM to make rounding
244            * easier.
245            */
246           freq = (freq * 1000 + 10700001) / 12500;
247           (void)udsbr_req(sc, 0x01, (freq >> 8) & 0xff, freq & 0xff);
248           (void)udsbr_req(sc, 0x00, 0x0096, 0x00b7);
249           usbd_delay_ms(sc->sc_udev, 240); /* wait for signal to settle */
250 }
251 
252 int
udsbr_status(struct udsbr_softc * sc)253 udsbr_status(struct udsbr_softc *sc)
254 {
255           return udsbr_req(sc, 0x00, 0x0000, 0x0024);
256 }
257 
258 
259 int
udsbr_get_info(void * v,struct radio_info * ri)260 udsbr_get_info(void *v, struct radio_info *ri)
261 {
262           struct udsbr_softc *sc = v;
263 
264           ri->mute = sc->sc_mute;
265           ri->volume = sc->sc_vol ? 255 : 0;
266           ri->caps = RADIO_CAPS_DETECT_STEREO;
267           ri->rfreq = 0;
268           ri->lock = 0;
269           ri->freq = sc->sc_freq;
270           ri->info = udsbr_status(sc) ? RADIO_INFO_STEREO : 0;
271 
272           return 0;
273 }
274 
275 int
udsbr_set_info(void * v,struct radio_info * ri)276 udsbr_set_info(void *v, struct radio_info *ri)
277 {
278           struct udsbr_softc *sc = v;
279 
280           sc->sc_mute = ri->mute != 0;
281           sc->sc_vol = ri->volume != 0;
282           sc->sc_freq = ri->freq;
283           udsbr_setfreq(sc, sc->sc_freq);
284 
285           if (sc->sc_mute || sc->sc_vol == 0)
286                     udsbr_stop(sc);
287           else
288                     udsbr_start(sc);
289 
290           return 0;
291 }
292