36aa32d4bfd2a27baca7b4e3f35fb4bd80bc6ea3
[dragonfly.git] / sys / dev / usbmisc / ufm / ufm.c
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
31 /*
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.22 2007/11/05 19:09:43 hasso Exp $
34  */
35
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/poll.h>
51 #include <sys/sysctl.h>
52 #include <sys/thread2.h>
53
54 #include <bus/usb/usb.h>
55 #include <bus/usb/usbdi.h>
56 #include <bus/usb/usbdi_util.h>
57
58 #include <bus/usb/dsbr100io.h>
59
60 #ifdef USB_DEBUG
61 #define DPRINTF(x)      if (ufmdebug) kprintf x
62 #define DPRINTFN(n,x)   if (ufmdebug>(n)) kprintf x
63 int     ufmdebug = 0;
64 SYSCTL_NODE(_hw_usb, OID_AUTO, ufm, CTLFLAG_RW, 0, "USB ufm");
65 SYSCTL_INT(_hw_usb_ufm, OID_AUTO, debug, CTLFLAG_RW,
66            &ufmdebug, 0, "ufm debug level");
67 #else
68 #define DPRINTF(x)
69 #define DPRINTFN(n,x)
70 #endif
71
72 d_open_t  ufmopen;
73 d_close_t ufmclose;
74 d_ioctl_t ufmioctl;
75
76 #define UFM_CDEV_MAJOR  200
77
78 static struct dev_ops ufm_ops = {
79         { "ufm", UFM_CDEV_MAJOR, 0 },
80         .d_open = ufmopen,
81         .d_close = ufmclose,
82         .d_ioctl = ufmioctl,
83 };
84
85 #define FM_CMD0         0x00
86 #define FM_CMD_SET_FREQ 0x01
87 #define FM_CMD2         0x02
88
89 struct ufm_softc {
90         device_t sc_dev;
91         usbd_device_handle sc_udev;
92         usbd_interface_handle sc_iface;
93
94         int sc_opened;
95         int sc_epaddr;
96         int sc_freq;
97
98         int sc_refcnt;
99 };
100
101 #define UFMUNIT(n) (minor(n))
102
103 static device_probe_t ufm_match;
104 static device_attach_t ufm_attach;
105 static device_detach_t ufm_detach;
106
107 static devclass_t ufm_devclass;
108
109 static kobj_method_t ufm_methods[] = {
110         DEVMETHOD(device_probe, ufm_match),
111         DEVMETHOD(device_attach, ufm_attach),
112         DEVMETHOD(device_detach, ufm_detach),
113         {0,0}
114 };
115
116 static driver_t ufm_driver = {
117         "ufm",
118         ufm_methods,
119         sizeof(struct ufm_softc)
120 };
121
122 MODULE_DEPEND(ufm, usb, 1, 1, 1);
123
124 static int
125 ufm_match(device_t self)
126 {
127         struct usb_attach_arg *uaa = device_get_ivars(self);
128         usb_device_descriptor_t *dd;
129
130         DPRINTFN(10,("ufm_match\n"));
131         if (!uaa->iface)
132                 return UMATCH_NONE;
133
134         dd = usbd_get_device_descriptor(uaa->device);
135
136         if (dd &&
137             ((UGETW(dd->idVendor) == 0x04b4 && UGETW(dd->idProduct) == 0x1002)))
138                 return UMATCH_VENDOR_PRODUCT;
139         else
140                 return UMATCH_NONE;
141 }
142
143 static int
144 ufm_attach(device_t self)
145 {
146         struct ufm_softc *sc = device_get_softc(self);
147         struct usb_attach_arg *uaa = device_get_ivars(self);
148         char devinfo[1024];
149         usb_endpoint_descriptor_t *edesc;
150         usbd_device_handle udev;
151         usbd_interface_handle iface;
152         u_int8_t epcount;
153         usbd_status r;
154         char * ermsg = "<none>";
155
156         DPRINTFN(10,("ufm_attach: sc=%p\n", sc));
157         usbd_devinfo(uaa->device, 0, devinfo);
158         sc->sc_dev = self;
159         device_set_desc_copy(self, devinfo);
160         kprintf("%s: %s\n", device_get_nameunit(sc->sc_dev), devinfo);
161
162         sc->sc_udev = udev = uaa->device;
163
164         if ((!uaa->device) || (!uaa->iface)) {
165                 ermsg = "device or iface";
166                 goto nobulk;
167         }
168         sc->sc_iface = iface = uaa->iface;
169         sc->sc_opened = 0;
170         sc->sc_refcnt = 0;
171
172         r = usbd_endpoint_count(iface, &epcount);
173         if (r != USBD_NORMAL_COMPLETION) {
174                 ermsg = "endpoints";
175                 goto nobulk;
176         }
177
178         edesc = usbd_interface2endpoint_descriptor(iface, 0);
179         if (!edesc) {
180                 ermsg = "interface endpoint";
181                 goto nobulk;
182         }
183         sc->sc_epaddr = edesc->bEndpointAddress;
184
185         /* XXX no error trapping, no storing of cdev_t */
186         dev_ops_add(&ufm_ops, -1, device_get_unit(self));
187         make_dev(&ufm_ops, device_get_unit(self),
188                         UID_ROOT, GID_OPERATOR,
189                         0644, "ufm%d", device_get_unit(self));
190
191         DPRINTFN(10, ("ufm_attach: %p\n", sc->sc_udev));
192
193         return 0;
194
195  nobulk:
196         kprintf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
197         return ENXIO;
198 }
199
200
201 int
202 ufmopen(struct dev_open_args *ap)
203 {
204         cdev_t dev = ap->a_head.a_dev;
205         struct ufm_softc *sc;
206
207         int unit = UFMUNIT(dev);
208         sc = devclass_get_softc(ufm_devclass, unit);
209         if (sc == NULL)
210                 return (ENXIO);
211
212         DPRINTFN(5, ("ufmopen: flag=%d, mode=%d, unit=%d\n",
213                      ap->a_oflags, ap->a_devtype, unit));
214
215         if (sc->sc_opened)
216                 return (EBUSY);
217
218         if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
219                 return (EACCES);
220
221         sc->sc_opened = 1;
222         return (0);
223 }
224
225 int
226 ufmclose(struct dev_close_args *ap)
227 {
228         cdev_t dev = ap->a_head.a_dev;
229         struct ufm_softc *sc;
230
231         int unit = UFMUNIT(dev);
232         sc = devclass_get_softc(ufm_devclass, unit);
233
234         DPRINTFN(5, ("ufmclose: flag=%d, mode=%d, unit=%d\n", 
235                     ap->a_fflag, ap->a_devtype, unit));
236         sc->sc_opened = 0;
237         sc->sc_refcnt = 0;
238         return 0;
239 }
240
241 static int
242 ufm_do_req(struct ufm_softc *sc, u_int8_t reqtype, u_int8_t request,
243     u_int16_t value, u_int16_t index, u_int8_t len, void *retbuf)
244 {
245         usb_device_request_t req;
246         usbd_status err;
247
248         crit_enter();
249         req.bmRequestType = reqtype;
250         req.bRequest = request;
251         USETW(req.wValue, value);
252         USETW(req.wIndex, index);
253         USETW(req.wLength, len);
254         err = usbd_do_request_flags(sc->sc_udev, &req, retbuf, 0, NULL,
255             USBD_DEFAULT_TIMEOUT);
256         crit_exit();
257         if (err) {
258                 kprintf("usbd_do_request_flags returned %#x\n", err);
259                 return (EIO);
260         }
261         return (0);
262 }
263
264 static int
265 ufm_set_freq(struct ufm_softc *sc, caddr_t addr)
266 {
267         int freq = *(int *)addr;
268         u_int8_t ret;
269
270         /*
271          * Freq now is in Hz.  We need to convert it to the frequency
272          * that the radio wants.  This frequency is 10.7MHz above
273          * the actual frequency.  We then need to convert to
274          * units of 12.5kHz.  We add one to the IFM to make rounding
275          * easier.
276          */
277         sc->sc_freq = freq;
278         freq = (freq + 10700001) / 12500;
279         /* This appears to set the frequency */
280         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD_SET_FREQ, freq >> 8,
281             freq, 1, &ret) != 0)
282                 return (EIO);
283         /* Not sure what this does */
284         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x96, 0xb7, 1,
285             &ret) != 0)
286                 return (EIO);
287         return (0);
288 }
289
290 static int
291 ufm_get_freq(struct ufm_softc *sc, caddr_t addr)
292 {
293         int *valp = (int *)addr;
294         *valp = sc->sc_freq;
295         return (0);
296 }
297
298 static int
299 ufm_start(struct ufm_softc *sc, caddr_t addr)
300 {
301         u_int8_t ret;
302
303         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0xc7,
304             1, &ret))
305                 return (EIO);
306         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x01, 0x00,
307             1, &ret))
308                 return (EIO);
309         if (ret & 0x1)
310                 return (EIO);
311         return (0);
312 }
313
314 static int
315 ufm_stop(struct ufm_softc *sc, caddr_t addr)
316 {
317         u_int8_t ret;
318
319         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x16, 0x1C,
320             1, &ret))
321                 return (EIO);
322         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x00, 0x00,
323             1, &ret))
324                 return (EIO);
325         return (0);
326 }
327
328 static int
329 ufm_get_stat(struct ufm_softc *sc, caddr_t addr)
330 {
331         u_int8_t ret;
332
333         /*
334          * Note, there's a 240ms settle time before the status
335          * will be valid, so tsleep that amount.  hz/4 is a good
336          * approximation of that.  Since this is a short sleep
337          * we don't try to catch any signals to keep things
338          * simple.
339          */
340         tsleep(sc, 0, "ufmwait", hz/4);
341         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0x24,
342             1, &ret))
343                 return (EIO);
344         *(int *)addr = ret;
345
346         return (0);
347 }
348
349 int
350 ufmioctl(struct dev_ioctl_args *ap)
351 {
352         cdev_t dev = ap->a_head.a_dev;
353         caddr_t addr = ap->a_data;
354         struct ufm_softc *sc;
355
356         int unit = UFMUNIT(dev);
357         int error = 0;
358
359         sc = devclass_get_softc(ufm_devclass, unit);
360
361         switch (ap->a_cmd) {
362         case FM_SET_FREQ:
363                 error = ufm_set_freq(sc, addr);
364                 break;
365         case FM_GET_FREQ:
366                 error = ufm_get_freq(sc, addr);
367                 break;
368         case FM_START:
369                 error = ufm_start(sc, addr);
370                 break;
371         case FM_STOP:
372                 error = ufm_stop(sc, addr);
373                 break;
374         case FM_GET_STAT:
375                 error = ufm_get_stat(sc, addr);
376                 break;
377         default:
378                 return ENOTTY;
379                 break;
380         }
381         return error;
382 }
383
384 static int
385 ufm_detach(device_t self)
386 {
387         DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
388         return 0;
389 }
390
391 DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, usbd_driver_load, 0);