Fix a bug-a-boo, the type uuid was being printed instead of the storage
[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 $
7c070ee7 33 * $DragonFly: src/sys/dev/usbmisc/ufm/ufm.c,v 1.14 2007/06/27 12:28:00 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
63#define DPRINTF(x) if (ufmdebug) logprintf x
64#define DPRINTFN(n,x) if (ufmdebug>(n)) logprintf x
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
fef8985e
MD
80Static struct dev_ops ufm_ops = {
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 {
92 USBBASEDEVICE sc_dev;
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
107USB_MATCH(ufm)
108{
109 USB_MATCH_START(ufm, uaa);
110 usb_device_descriptor_t *dd;
111
112 DPRINTFN(10,("ufm_match\n"));
113 if (!uaa->iface)
114 return UMATCH_NONE;
115
116 dd = usbd_get_device_descriptor(uaa->device);
117
118 if (dd &&
119 ((UGETW(dd->idVendor) == USB_VENDOR_CYPRESS &&
120 UGETW(dd->idProduct) == USB_PRODUCT_CYPRESS_FMRADIO)))
121 return UMATCH_VENDOR_PRODUCT;
122 else
123 return UMATCH_NONE;
124}
125
126USB_ATTACH(ufm)
127{
128 USB_ATTACH_START(ufm, sc, uaa);
129 char devinfo[1024];
130 usb_endpoint_descriptor_t *edesc;
131 usbd_device_handle udev;
132 usbd_interface_handle iface;
133 u_int8_t epcount;
984263bc
MD
134 usbd_status r;
135 char * ermsg = "<none>";
136
1550dfd9 137 DPRINTFN(10,("ufm_attach: sc=%p\n", sc));
984263bc
MD
138 usbd_devinfo(uaa->device, 0, devinfo);
139 USB_ATTACH_SETUP;
e3869ec7 140 kprintf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
984263bc
MD
141
142 sc->sc_udev = udev = uaa->device;
143
984263bc
MD
144 if ((!uaa->device) || (!uaa->iface)) {
145 ermsg = "device or iface";
146 goto nobulk;
147 }
148 sc->sc_iface = iface = uaa->iface;
984263bc
MD
149 sc->sc_opened = 0;
150 sc->sc_refcnt = 0;
151
152 r = usbd_endpoint_count(iface, &epcount);
1550dfd9 153 if (r != USBD_NORMAL_COMPLETION) {
984263bc
MD
154 ermsg = "endpoints";
155 goto nobulk;
156 }
157
158 edesc = usbd_interface2endpoint_descriptor(iface, 0);
159 if (!edesc) {
160 ermsg = "interface endpoint";
161 goto nobulk;
162 }
163 sc->sc_epaddr = edesc->bEndpointAddress;
164
b13267a5 165 /* XXX no error trapping, no storing of cdev_t */
fef8985e
MD
166 dev_ops_add(&ufm_ops, -1, device_get_unit(self));
167 make_dev(&ufm_ops, device_get_unit(self),
984263bc
MD
168 UID_ROOT, GID_OPERATOR,
169 0644, "ufm%d", device_get_unit(self));
984263bc
MD
170
171 DPRINTFN(10, ("ufm_attach: %p\n", sc->sc_udev));
172
173 USB_ATTACH_SUCCESS_RETURN;
174
175 nobulk:
e3869ec7 176 kprintf("%s: could not find %s\n", USBDEVNAME(sc->sc_dev),ermsg);
984263bc
MD
177 USB_ATTACH_ERROR_RETURN;
178}
179
180
181int
fef8985e 182ufmopen(struct dev_open_args *ap)
984263bc 183{
b13267a5 184 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
185 struct ufm_softc *sc;
186
187 int unit = UFMUNIT(dev);
188 USB_GET_SC_OPEN(ufm, unit, sc);
189
1550dfd9 190 DPRINTFN(5, ("ufmopen: flag=%d, mode=%d, unit=%d\n",
fef8985e 191 ap->a_oflags, ap->a_devtype, unit));
984263bc
MD
192
193 if (sc->sc_opened)
194 return (EBUSY);
195
fef8985e 196 if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
984263bc
MD
197 return (EACCES);
198
199 sc->sc_opened = 1;
200 return (0);
201}
202
203int
fef8985e 204ufmclose(struct dev_close_args *ap)
984263bc 205{
b13267a5 206 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
207 struct ufm_softc *sc;
208
209 int unit = UFMUNIT(dev);
210 USB_GET_SC(ufm, unit, sc);
211
fef8985e
MD
212 DPRINTFN(5, ("ufmclose: flag=%d, mode=%d, unit=%d\n",
213 ap->a_fflag, ap->a_devtype, unit));
984263bc
MD
214 sc->sc_opened = 0;
215 sc->sc_refcnt = 0;
1550dfd9 216 return 0;
984263bc
MD
217}
218
219static int
220ufm_do_req(struct ufm_softc *sc, u_int8_t reqtype, u_int8_t request,
221 u_int16_t value, u_int16_t index, u_int8_t len, void *retbuf)
222{
984263bc
MD
223 usb_device_request_t req;
224 usbd_status err;
225
4e01b467 226 crit_enter();
984263bc
MD
227 req.bmRequestType = reqtype;
228 req.bRequest = request;
229 USETW(req.wValue, value);
230 USETW(req.wIndex, index);
231 USETW(req.wLength, len);
1550dfd9
MD
232 err = usbd_do_request_flags(sc->sc_udev, &req, retbuf, 0, NULL,
233 USBD_DEFAULT_TIMEOUT);
4e01b467 234 crit_exit();
984263bc 235 if (err) {
e3869ec7 236 kprintf("usbd_do_request_flags returned %#x\n", err);
984263bc
MD
237 return (EIO);
238 }
239 return (0);
240}
241
242static int
243ufm_set_freq(struct ufm_softc *sc, caddr_t addr)
244{
245 int freq = *(int *)addr;
246 u_int8_t ret;
247
248 /*
249 * Freq now is in Hz. We need to convert it to the frequency
250 * that the radio wants. This frequency is 10.7MHz above
251 * the actual frequency. We then need to convert to
252 * units of 12.5kHz. We add one to the IFM to make rounding
1550dfd9 253 * easier.
984263bc
MD
254 */
255 sc->sc_freq = freq;
256 freq = (freq + 10700001) / 12500;
257 /* This appears to set the frequency */
258 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD_SET_FREQ, freq >> 8,
259 freq, 1, &ret) != 0)
260 return (EIO);
261 /* Not sure what this does */
1550dfd9 262 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x96, 0xb7, 1,
984263bc
MD
263 &ret) != 0)
264 return (EIO);
265 return (0);
266}
267
268static int
269ufm_get_freq(struct ufm_softc *sc, caddr_t addr)
270{
271 int *valp = (int *)addr;
272 *valp = sc->sc_freq;
273 return (0);
274}
275
276static int
277ufm_start(struct ufm_softc *sc, caddr_t addr)
278{
279 u_int8_t ret;
1550dfd9
MD
280
281 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0xc7,
984263bc
MD
282 1, &ret))
283 return (EIO);
284 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x01, 0x00,
285 1, &ret))
286 return (EIO);
287 if (ret & 0x1)
288 return (EIO);
289 return (0);
290}
291
292static int
293ufm_stop(struct ufm_softc *sc, caddr_t addr)
294{
295 u_int8_t ret;
1550dfd9 296
984263bc
MD
297 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x16, 0x1C,
298 1, &ret))
299 return (EIO);
300 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x00, 0x00,
301 1, &ret))
302 return (EIO);
303 return (0);
304}
305
306static int
307ufm_get_stat(struct ufm_softc *sc, caddr_t addr)
308{
309 u_int8_t ret;
1550dfd9 310
984263bc
MD
311 /*
312 * Note, there's a 240ms settle time before the status
313 * will be valid, so tsleep that amount. hz/4 is a good
314 * approximation of that. Since this is a short sleep
315 * we don't try to catch any signals to keep things
316 * simple.
317 */
318 tsleep(sc, 0, "ufmwait", hz/4);
319 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0x24,
320 1, &ret))
321 return (EIO);
322 *(int *)addr = ret;
1550dfd9 323
984263bc
MD
324 return (0);
325}
326
327int
fef8985e 328ufmioctl(struct dev_ioctl_args *ap)
984263bc 329{
b13267a5 330 cdev_t dev = ap->a_head.a_dev;
fef8985e 331 caddr_t addr = ap->a_data;
984263bc
MD
332 struct ufm_softc *sc;
333
334 int unit = UFMUNIT(dev);
335 int error = 0;
336
337 USB_GET_SC(ufm, unit, sc);
338
fef8985e 339 switch (ap->a_cmd) {
984263bc
MD
340 case FM_SET_FREQ:
341 error = ufm_set_freq(sc, addr);
342 break;
343 case FM_GET_FREQ:
344 error = ufm_get_freq(sc, addr);
345 break;
346 case FM_START:
347 error = ufm_start(sc, addr);
348 break;
349 case FM_STOP:
350 error = ufm_stop(sc, addr);
351 break;
352 case FM_GET_STAT:
353 error = ufm_get_stat(sc, addr);
354 break;
355 default:
356 return ENOTTY;
357 break;
358 }
359 return error;
360}
361
984263bc
MD
362Static int
363ufm_detach(device_t self)
1550dfd9 364{
984263bc
MD
365 DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
366 return 0;
367}
368
369DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, usbd_driver_load, 0);