Oops, forgot space.
[dragonfly.git] / sys / dev / usbmisc / ufm / ufm.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 2001 M. Warner Losh
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
18 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * This code is based on ugen.c and ulpt.c developed by Lennart Augustsson.
27 * This code includes software developed by the NetBSD Foundation, Inc. and
28 * its contributors.
29 */
30
1550dfd9
MD
31/*
32 * $FreeBSD: src/sys/dev/usb/ufm.c,v 1.16 2003/10/04 21:41:01 joe Exp $
1e089e7e 33 * $DragonFly: src/sys/dev/usbmisc/ufm/ufm.c,v 1.18 2007/07/02 06:43:31 hasso Exp $
1550dfd9 34 */
984263bc
MD
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/kernel.h>
39#include <sys/malloc.h>
984263bc
MD
40#include <sys/module.h>
41#include <sys/bus.h>
42#include <sys/ioccom.h>
984263bc
MD
43#include <sys/fcntl.h>
44#include <sys/filio.h>
45#include <sys/conf.h>
46#include <sys/uio.h>
47#include <sys/tty.h>
48#include <sys/file.h>
984263bc 49#include <sys/select.h>
984263bc
MD
50#include <sys/vnode.h>
51#include <sys/poll.h>
52#include <sys/sysctl.h>
4e01b467 53#include <sys/thread2.h>
984263bc 54
1f2de5d4
MD
55#include <bus/usb/usb.h>
56#include <bus/usb/usbdi.h>
57#include <bus/usb/usbdi_util.h>
984263bc 58
1f2de5d4
MD
59#include <bus/usb/usbdevs.h>
60#include <bus/usb/dsbr100io.h>
984263bc
MD
61
62#ifdef USB_DEBUG
fc1ef497
HT
63#define DPRINTF(x) if (ufmdebug) kprintf x
64#define DPRINTFN(n,x) if (ufmdebug>(n)) kprintf x
984263bc
MD
65int ufmdebug = 0;
66SYSCTL_NODE(_hw_usb, OID_AUTO, ufm, CTLFLAG_RW, 0, "USB ufm");
67SYSCTL_INT(_hw_usb_ufm, OID_AUTO, debug, CTLFLAG_RW,
68 &ufmdebug, 0, "ufm debug level");
69#else
70#define DPRINTF(x)
71#define DPRINTFN(n,x)
72#endif
73
984263bc
MD
74d_open_t ufmopen;
75d_close_t ufmclose;
76d_ioctl_t ufmioctl;
77
78#define UFM_CDEV_MAJOR 200
79
6ed427ca 80static struct dev_ops ufm_ops = {
fef8985e
MD
81 { "ufm", UFM_CDEV_MAJOR, 0 },
82 .d_open = ufmopen,
83 .d_close = ufmclose,
84 .d_ioctl = ufmioctl,
984263bc 85};
984263bc
MD
86
87#define FM_CMD0 0x00
88#define FM_CMD_SET_FREQ 0x01
89#define FM_CMD2 0x02
90
91struct ufm_softc {
6ed427ca 92 device_t sc_dev;
984263bc
MD
93 usbd_device_handle sc_udev;
94 usbd_interface_handle sc_iface;
95
96 int sc_opened;
97 int sc_epaddr;
98 int sc_freq;
99
100 int sc_refcnt;
984263bc
MD
101};
102
103#define UFMUNIT(n) (minor(n))
104
105USB_DECLARE_DRIVER(ufm);
106
e785a5d9
HT
107static int
108ufm_match(device_t self)
984263bc 109{
e785a5d9 110 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc
MD
111 usb_device_descriptor_t *dd;
112
113 DPRINTFN(10,("ufm_match\n"));
114 if (!uaa->iface)
115 return UMATCH_NONE;
116
117 dd = usbd_get_device_descriptor(uaa->device);
118
119 if (dd &&
120 ((UGETW(dd->idVendor) == USB_VENDOR_CYPRESS &&
121 UGETW(dd->idProduct) == USB_PRODUCT_CYPRESS_FMRADIO)))
122 return UMATCH_VENDOR_PRODUCT;
123 else
124 return UMATCH_NONE;
125}
126
e785a5d9
HT
127static int
128ufm_attach(device_t self)
984263bc 129{
e785a5d9
HT
130 struct ufm_softc *sc = device_get_softc(self);
131 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc
MD
132 char devinfo[1024];
133 usb_endpoint_descriptor_t *edesc;
134 usbd_device_handle udev;
135 usbd_interface_handle iface;
136 u_int8_t epcount;
984263bc
MD
137 usbd_status r;
138 char * ermsg = "<none>";
139
1550dfd9 140 DPRINTFN(10,("ufm_attach: sc=%p\n", sc));
984263bc 141 usbd_devinfo(uaa->device, 0, devinfo);
e785a5d9
HT
142 sc->sc_dev = self;
143 device_set_desc_copy(self, devinfo);
6ed427ca 144 kprintf("%s: %s\n", device_get_nameunit(sc->sc_dev), devinfo);
984263bc
MD
145
146 sc->sc_udev = udev = uaa->device;
147
984263bc
MD
148 if ((!uaa->device) || (!uaa->iface)) {
149 ermsg = "device or iface";
150 goto nobulk;
151 }
152 sc->sc_iface = iface = uaa->iface;
984263bc
MD
153 sc->sc_opened = 0;
154 sc->sc_refcnt = 0;
155
156 r = usbd_endpoint_count(iface, &epcount);
1550dfd9 157 if (r != USBD_NORMAL_COMPLETION) {
984263bc
MD
158 ermsg = "endpoints";
159 goto nobulk;
160 }
161
162 edesc = usbd_interface2endpoint_descriptor(iface, 0);
163 if (!edesc) {
164 ermsg = "interface endpoint";
165 goto nobulk;
166 }
167 sc->sc_epaddr = edesc->bEndpointAddress;
168
b13267a5 169 /* XXX no error trapping, no storing of cdev_t */
fef8985e
MD
170 dev_ops_add(&ufm_ops, -1, device_get_unit(self));
171 make_dev(&ufm_ops, device_get_unit(self),
984263bc
MD
172 UID_ROOT, GID_OPERATOR,
173 0644, "ufm%d", device_get_unit(self));
984263bc
MD
174
175 DPRINTFN(10, ("ufm_attach: %p\n", sc->sc_udev));
176
e785a5d9 177 return 0;
984263bc
MD
178
179 nobulk:
6ed427ca 180 kprintf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
e785a5d9 181 return ENXIO;
984263bc
MD
182}
183
184
185int
fef8985e 186ufmopen(struct dev_open_args *ap)
984263bc 187{
b13267a5 188 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
189 struct ufm_softc *sc;
190
191 int unit = UFMUNIT(dev);
1e089e7e
HT
192 sc = devclass_get_softc(ufm_devclass, unit);
193 if (sc == NULL)
194 return (ENXIO);
984263bc 195
1550dfd9 196 DPRINTFN(5, ("ufmopen: flag=%d, mode=%d, unit=%d\n",
fef8985e 197 ap->a_oflags, ap->a_devtype, unit));
984263bc
MD
198
199 if (sc->sc_opened)
200 return (EBUSY);
201
fef8985e 202 if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
984263bc
MD
203 return (EACCES);
204
205 sc->sc_opened = 1;
206 return (0);
207}
208
209int
fef8985e 210ufmclose(struct dev_close_args *ap)
984263bc 211{
b13267a5 212 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
213 struct ufm_softc *sc;
214
215 int unit = UFMUNIT(dev);
1e089e7e 216 sc = devclass_get_softc(ufm_devclass, unit);
984263bc 217
fef8985e
MD
218 DPRINTFN(5, ("ufmclose: flag=%d, mode=%d, unit=%d\n",
219 ap->a_fflag, ap->a_devtype, unit));
984263bc
MD
220 sc->sc_opened = 0;
221 sc->sc_refcnt = 0;
1550dfd9 222 return 0;
984263bc
MD
223}
224
225static int
226ufm_do_req(struct ufm_softc *sc, u_int8_t reqtype, u_int8_t request,
227 u_int16_t value, u_int16_t index, u_int8_t len, void *retbuf)
228{
984263bc
MD
229 usb_device_request_t req;
230 usbd_status err;
231
4e01b467 232 crit_enter();
984263bc
MD
233 req.bmRequestType = reqtype;
234 req.bRequest = request;
235 USETW(req.wValue, value);
236 USETW(req.wIndex, index);
237 USETW(req.wLength, len);
1550dfd9
MD
238 err = usbd_do_request_flags(sc->sc_udev, &req, retbuf, 0, NULL,
239 USBD_DEFAULT_TIMEOUT);
4e01b467 240 crit_exit();
984263bc 241 if (err) {
e3869ec7 242 kprintf("usbd_do_request_flags returned %#x\n", err);
984263bc
MD
243 return (EIO);
244 }
245 return (0);
246}
247
248static int
249ufm_set_freq(struct ufm_softc *sc, caddr_t addr)
250{
251 int freq = *(int *)addr;
252 u_int8_t ret;
253
254 /*
255 * Freq now is in Hz. We need to convert it to the frequency
256 * that the radio wants. This frequency is 10.7MHz above
257 * the actual frequency. We then need to convert to
258 * units of 12.5kHz. We add one to the IFM to make rounding
1550dfd9 259 * easier.
984263bc
MD
260 */
261 sc->sc_freq = freq;
262 freq = (freq + 10700001) / 12500;
263 /* This appears to set the frequency */
264 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD_SET_FREQ, freq >> 8,
265 freq, 1, &ret) != 0)
266 return (EIO);
267 /* Not sure what this does */
1550dfd9 268 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x96, 0xb7, 1,
984263bc
MD
269 &ret) != 0)
270 return (EIO);
271 return (0);
272}
273
274static int
275ufm_get_freq(struct ufm_softc *sc, caddr_t addr)
276{
277 int *valp = (int *)addr;
278 *valp = sc->sc_freq;
279 return (0);
280}
281
282static int
283ufm_start(struct ufm_softc *sc, caddr_t addr)
284{
285 u_int8_t ret;
1550dfd9
MD
286
287 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0xc7,
984263bc
MD
288 1, &ret))
289 return (EIO);
290 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x01, 0x00,
291 1, &ret))
292 return (EIO);
293 if (ret & 0x1)
294 return (EIO);
295 return (0);
296}
297
298static int
299ufm_stop(struct ufm_softc *sc, caddr_t addr)
300{
301 u_int8_t ret;
1550dfd9 302
984263bc
MD
303 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x16, 0x1C,
304 1, &ret))
305 return (EIO);
306 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x00, 0x00,
307 1, &ret))
308 return (EIO);
309 return (0);
310}
311
312static int
313ufm_get_stat(struct ufm_softc *sc, caddr_t addr)
314{
315 u_int8_t ret;
1550dfd9 316
984263bc
MD
317 /*
318 * Note, there's a 240ms settle time before the status
319 * will be valid, so tsleep that amount. hz/4 is a good
320 * approximation of that. Since this is a short sleep
321 * we don't try to catch any signals to keep things
322 * simple.
323 */
324 tsleep(sc, 0, "ufmwait", hz/4);
325 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0x24,
326 1, &ret))
327 return (EIO);
328 *(int *)addr = ret;
1550dfd9 329
984263bc
MD
330 return (0);
331}
332
333int
fef8985e 334ufmioctl(struct dev_ioctl_args *ap)
984263bc 335{
b13267a5 336 cdev_t dev = ap->a_head.a_dev;
fef8985e 337 caddr_t addr = ap->a_data;
984263bc
MD
338 struct ufm_softc *sc;
339
340 int unit = UFMUNIT(dev);
341 int error = 0;
342
1e089e7e 343 sc = devclass_get_softc(ufm_devclass, unit);
984263bc 344
fef8985e 345 switch (ap->a_cmd) {
984263bc
MD
346 case FM_SET_FREQ:
347 error = ufm_set_freq(sc, addr);
348 break;
349 case FM_GET_FREQ:
350 error = ufm_get_freq(sc, addr);
351 break;
352 case FM_START:
353 error = ufm_start(sc, addr);
354 break;
355 case FM_STOP:
356 error = ufm_stop(sc, addr);
357 break;
358 case FM_GET_STAT:
359 error = ufm_get_stat(sc, addr);
360 break;
361 default:
362 return ENOTTY;
363 break;
364 }
365 return error;
366}
367
6ed427ca 368static int
984263bc 369ufm_detach(device_t self)
1550dfd9 370{
6ed427ca 371 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
984263bc
MD
372 return 0;
373}
374
375DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, usbd_driver_load, 0);