Nuke USB_DECLARE_DRIVER and USB_DECLARE_DRIVER_INIT macros.
[dragonfly/port-amd64.git] / sys / dev / usbmisc / ufm / ufm.c
blob2995d64758d69d8993aed961c0bd834ee0ef045a
1 /*
2 * Copyright (c) 2001 M. Warner Losh
3 * All rights reserved.
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.
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.
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.
32 * $FreeBSD: src/sys/dev/usb/ufm.c,v 1.16 2003/10/04 21:41:01 joe Exp $
33 * $DragonFly: src/sys/dev/usbmisc/ufm/ufm.c,v 1.19 2007/07/02 23:52:05 hasso Exp $
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 #include <sys/ioccom.h>
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>
49 #include <sys/select.h>
50 #include <sys/vnode.h>
51 #include <sys/poll.h>
52 #include <sys/sysctl.h>
53 #include <sys/thread2.h>
55 #include <bus/usb/usb.h>
56 #include <bus/usb/usbdi.h>
57 #include <bus/usb/usbdi_util.h>
59 #include <bus/usb/usbdevs.h>
60 #include <bus/usb/dsbr100io.h>
62 #ifdef USB_DEBUG
63 #define DPRINTF(x) if (ufmdebug) kprintf x
64 #define DPRINTFN(n,x) if (ufmdebug>(n)) kprintf x
65 int ufmdebug = 0;
66 SYSCTL_NODE(_hw_usb, OID_AUTO, ufm, CTLFLAG_RW, 0, "USB ufm");
67 SYSCTL_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
74 d_open_t ufmopen;
75 d_close_t ufmclose;
76 d_ioctl_t ufmioctl;
78 #define UFM_CDEV_MAJOR 200
80 static struct dev_ops ufm_ops = {
81 { "ufm", UFM_CDEV_MAJOR, 0 },
82 .d_open = ufmopen,
83 .d_close = ufmclose,
84 .d_ioctl = ufmioctl,
87 #define FM_CMD0 0x00
88 #define FM_CMD_SET_FREQ 0x01
89 #define FM_CMD2 0x02
91 struct ufm_softc {
92 device_t sc_dev;
93 usbd_device_handle sc_udev;
94 usbd_interface_handle sc_iface;
96 int sc_opened;
97 int sc_epaddr;
98 int sc_freq;
100 int sc_refcnt;
103 #define UFMUNIT(n) (minor(n))
105 static device_probe_t ufm_match;
106 static device_attach_t ufm_attach;
107 static device_detach_t ufm_detach;
109 static devclass_t ufm_devclass;
111 static kobj_method_t ufm_methods[] = {
112 DEVMETHOD(device_probe, ufm_match),
113 DEVMETHOD(device_attach, ufm_attach),
114 DEVMETHOD(device_detach, ufm_detach),
115 {0,0}
118 static driver_t ufm_driver = {
119 "ufm",
120 ufm_methods,
121 sizeof(struct ufm_softc)
124 MODULE_DEPEND(ufm, usb, 1, 1, 1);
126 static int
127 ufm_match(device_t self)
129 struct usb_attach_arg *uaa = device_get_ivars(self);
130 usb_device_descriptor_t *dd;
132 DPRINTFN(10,("ufm_match\n"));
133 if (!uaa->iface)
134 return UMATCH_NONE;
136 dd = usbd_get_device_descriptor(uaa->device);
138 if (dd &&
139 ((UGETW(dd->idVendor) == USB_VENDOR_CYPRESS &&
140 UGETW(dd->idProduct) == USB_PRODUCT_CYPRESS_FMRADIO)))
141 return UMATCH_VENDOR_PRODUCT;
142 else
143 return UMATCH_NONE;
146 static int
147 ufm_attach(device_t self)
149 struct ufm_softc *sc = device_get_softc(self);
150 struct usb_attach_arg *uaa = device_get_ivars(self);
151 char devinfo[1024];
152 usb_endpoint_descriptor_t *edesc;
153 usbd_device_handle udev;
154 usbd_interface_handle iface;
155 u_int8_t epcount;
156 usbd_status r;
157 char * ermsg = "<none>";
159 DPRINTFN(10,("ufm_attach: sc=%p\n", sc));
160 usbd_devinfo(uaa->device, 0, devinfo);
161 sc->sc_dev = self;
162 device_set_desc_copy(self, devinfo);
163 kprintf("%s: %s\n", device_get_nameunit(sc->sc_dev), devinfo);
165 sc->sc_udev = udev = uaa->device;
167 if ((!uaa->device) || (!uaa->iface)) {
168 ermsg = "device or iface";
169 goto nobulk;
171 sc->sc_iface = iface = uaa->iface;
172 sc->sc_opened = 0;
173 sc->sc_refcnt = 0;
175 r = usbd_endpoint_count(iface, &epcount);
176 if (r != USBD_NORMAL_COMPLETION) {
177 ermsg = "endpoints";
178 goto nobulk;
181 edesc = usbd_interface2endpoint_descriptor(iface, 0);
182 if (!edesc) {
183 ermsg = "interface endpoint";
184 goto nobulk;
186 sc->sc_epaddr = edesc->bEndpointAddress;
188 /* XXX no error trapping, no storing of cdev_t */
189 dev_ops_add(&ufm_ops, -1, device_get_unit(self));
190 make_dev(&ufm_ops, device_get_unit(self),
191 UID_ROOT, GID_OPERATOR,
192 0644, "ufm%d", device_get_unit(self));
194 DPRINTFN(10, ("ufm_attach: %p\n", sc->sc_udev));
196 return 0;
198 nobulk:
199 kprintf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
200 return ENXIO;
205 ufmopen(struct dev_open_args *ap)
207 cdev_t dev = ap->a_head.a_dev;
208 struct ufm_softc *sc;
210 int unit = UFMUNIT(dev);
211 sc = devclass_get_softc(ufm_devclass, unit);
212 if (sc == NULL)
213 return (ENXIO);
215 DPRINTFN(5, ("ufmopen: flag=%d, mode=%d, unit=%d\n",
216 ap->a_oflags, ap->a_devtype, unit));
218 if (sc->sc_opened)
219 return (EBUSY);
221 if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
222 return (EACCES);
224 sc->sc_opened = 1;
225 return (0);
229 ufmclose(struct dev_close_args *ap)
231 cdev_t dev = ap->a_head.a_dev;
232 struct ufm_softc *sc;
234 int unit = UFMUNIT(dev);
235 sc = devclass_get_softc(ufm_devclass, unit);
237 DPRINTFN(5, ("ufmclose: flag=%d, mode=%d, unit=%d\n",
238 ap->a_fflag, ap->a_devtype, unit));
239 sc->sc_opened = 0;
240 sc->sc_refcnt = 0;
241 return 0;
244 static int
245 ufm_do_req(struct ufm_softc *sc, u_int8_t reqtype, u_int8_t request,
246 u_int16_t value, u_int16_t index, u_int8_t len, void *retbuf)
248 usb_device_request_t req;
249 usbd_status err;
251 crit_enter();
252 req.bmRequestType = reqtype;
253 req.bRequest = request;
254 USETW(req.wValue, value);
255 USETW(req.wIndex, index);
256 USETW(req.wLength, len);
257 err = usbd_do_request_flags(sc->sc_udev, &req, retbuf, 0, NULL,
258 USBD_DEFAULT_TIMEOUT);
259 crit_exit();
260 if (err) {
261 kprintf("usbd_do_request_flags returned %#x\n", err);
262 return (EIO);
264 return (0);
267 static int
268 ufm_set_freq(struct ufm_softc *sc, caddr_t addr)
270 int freq = *(int *)addr;
271 u_int8_t ret;
274 * Freq now is in Hz. We need to convert it to the frequency
275 * that the radio wants. This frequency is 10.7MHz above
276 * the actual frequency. We then need to convert to
277 * units of 12.5kHz. We add one to the IFM to make rounding
278 * easier.
280 sc->sc_freq = freq;
281 freq = (freq + 10700001) / 12500;
282 /* This appears to set the frequency */
283 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD_SET_FREQ, freq >> 8,
284 freq, 1, &ret) != 0)
285 return (EIO);
286 /* Not sure what this does */
287 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x96, 0xb7, 1,
288 &ret) != 0)
289 return (EIO);
290 return (0);
293 static int
294 ufm_get_freq(struct ufm_softc *sc, caddr_t addr)
296 int *valp = (int *)addr;
297 *valp = sc->sc_freq;
298 return (0);
301 static int
302 ufm_start(struct ufm_softc *sc, caddr_t addr)
304 u_int8_t ret;
306 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0xc7,
307 1, &ret))
308 return (EIO);
309 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x01, 0x00,
310 1, &ret))
311 return (EIO);
312 if (ret & 0x1)
313 return (EIO);
314 return (0);
317 static int
318 ufm_stop(struct ufm_softc *sc, caddr_t addr)
320 u_int8_t ret;
322 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x16, 0x1C,
323 1, &ret))
324 return (EIO);
325 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x00, 0x00,
326 1, &ret))
327 return (EIO);
328 return (0);
331 static int
332 ufm_get_stat(struct ufm_softc *sc, caddr_t addr)
334 u_int8_t ret;
337 * Note, there's a 240ms settle time before the status
338 * will be valid, so tsleep that amount. hz/4 is a good
339 * approximation of that. Since this is a short sleep
340 * we don't try to catch any signals to keep things
341 * simple.
343 tsleep(sc, 0, "ufmwait", hz/4);
344 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0x24,
345 1, &ret))
346 return (EIO);
347 *(int *)addr = ret;
349 return (0);
353 ufmioctl(struct dev_ioctl_args *ap)
355 cdev_t dev = ap->a_head.a_dev;
356 caddr_t addr = ap->a_data;
357 struct ufm_softc *sc;
359 int unit = UFMUNIT(dev);
360 int error = 0;
362 sc = devclass_get_softc(ufm_devclass, unit);
364 switch (ap->a_cmd) {
365 case FM_SET_FREQ:
366 error = ufm_set_freq(sc, addr);
367 break;
368 case FM_GET_FREQ:
369 error = ufm_get_freq(sc, addr);
370 break;
371 case FM_START:
372 error = ufm_start(sc, addr);
373 break;
374 case FM_STOP:
375 error = ufm_stop(sc, addr);
376 break;
377 case FM_GET_STAT:
378 error = ufm_get_stat(sc, addr);
379 break;
380 default:
381 return ENOTTY;
382 break;
384 return error;
387 static int
388 ufm_detach(device_t self)
390 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
391 return 0;
394 DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, usbd_driver_load, 0);