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