Merge from vendor branch LESS:
[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.12 2006/12/22 23:26:25 swildner Exp $
34  */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #if defined(__NetBSD__)
41 #include <sys/device.h>
42 #include <sys/ioctl.h>
43 #elif defined(__FreeBSD__) || defined(__DragonFly__)
44 #include <sys/module.h>
45 #include <sys/bus.h>
46 #include <sys/ioccom.h>
47 #endif
48 #include <sys/fcntl.h>
49 #include <sys/filio.h>
50 #include <sys/conf.h>
51 #include <sys/uio.h>
52 #include <sys/tty.h>
53 #include <sys/file.h>
54 #if defined(__FreeBSD__) && __FreeBSD_version >= 500014
55 #include <sys/selinfo.h>
56 #else
57 #include <sys/select.h>
58 #endif
59 #include <sys/vnode.h>
60 #include <sys/poll.h>
61 #include <sys/sysctl.h>
62 #include <sys/thread2.h>
63
64 #include <bus/usb/usb.h>
65 #include <bus/usb/usbdi.h>
66 #include <bus/usb/usbdi_util.h>
67
68 #include <bus/usb/usbdevs.h>
69 #include <bus/usb/dsbr100io.h>
70
71 #ifdef USB_DEBUG
72 #define DPRINTF(x)      if (ufmdebug) logprintf x
73 #define DPRINTFN(n,x)   if (ufmdebug>(n)) logprintf x
74 int     ufmdebug = 0;
75 SYSCTL_NODE(_hw_usb, OID_AUTO, ufm, CTLFLAG_RW, 0, "USB ufm");
76 SYSCTL_INT(_hw_usb_ufm, OID_AUTO, debug, CTLFLAG_RW,
77            &ufmdebug, 0, "ufm debug level");
78 #else
79 #define DPRINTF(x)
80 #define DPRINTFN(n,x)
81 #endif
82
83 #if defined(__NetBSD__) || defined(__OpenBSD__)
84 int ufmopen(cdev_t, int, int, usb_proc_ptr);
85 int ufmclose(cdev_t, int, int, usb_proc_ptr);
86 int ufmioctl(cdev_t, u_long, caddr_t, int, usb_proc_ptr);
87
88 cdev_decl(ufm);
89 #elif defined(__FreeBSD__) || defined(__DragonFly__)
90 d_open_t  ufmopen;
91 d_close_t ufmclose;
92 d_ioctl_t ufmioctl;
93
94 #define UFM_CDEV_MAJOR  200
95
96 Static struct dev_ops ufm_ops = {
97         { "ufm", UFM_CDEV_MAJOR, 0 },
98         .d_open = ufmopen,
99         .d_close = ufmclose,
100         .d_ioctl = ufmioctl,
101 };
102 #endif  /*defined(__FreeBSD__)*/
103
104 #define FM_CMD0         0x00
105 #define FM_CMD_SET_FREQ 0x01
106 #define FM_CMD2         0x02
107
108 struct ufm_softc {
109         USBBASEDEVICE sc_dev;
110         usbd_device_handle sc_udev;
111         usbd_interface_handle sc_iface;
112
113         int sc_opened;
114         int sc_epaddr;
115         int sc_freq;
116
117         int sc_refcnt;
118 #if defined(__NetBSD__) || defined(__OpenBSD__)
119         u_char sc_dying;
120 #endif
121 };
122
123 #define UFMUNIT(n) (minor(n))
124
125 USB_DECLARE_DRIVER(ufm);
126
127 USB_MATCH(ufm)
128 {
129         USB_MATCH_START(ufm, uaa);
130         usb_device_descriptor_t *dd;
131
132         DPRINTFN(10,("ufm_match\n"));
133         if (!uaa->iface)
134                 return UMATCH_NONE;
135
136         dd = usbd_get_device_descriptor(uaa->device);
137
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;
144 }
145
146 USB_ATTACH(ufm)
147 {
148         USB_ATTACH_START(ufm, sc, uaa);
149         char devinfo[1024];
150         usb_endpoint_descriptor_t *edesc;
151         usbd_device_handle udev;
152         usbd_interface_handle iface;
153         u_int8_t epcount;
154 #if defined(__NetBSD__) || defined(__OpenBSD__)
155         u_int8_t niface;
156 #endif
157         usbd_status r;
158         char * ermsg = "<none>";
159
160         DPRINTFN(10,("ufm_attach: sc=%p\n", sc));
161         usbd_devinfo(uaa->device, 0, devinfo);
162         USB_ATTACH_SETUP;
163         kprintf("%s: %s\n", USBDEVNAME(sc->sc_dev), devinfo);
164
165         sc->sc_udev = udev = uaa->device;
166
167 #if defined(__FreeBSD__) || defined(__DragonFly__)
168         if ((!uaa->device) || (!uaa->iface)) {
169                 ermsg = "device or iface";
170                 goto nobulk;
171         }
172         sc->sc_iface = iface = uaa->iface;
173 #elif defined(__NetBSD__) || defined(__OpenBSD__)
174         if (!udev) {
175                 ermsg = "device";
176                 goto nobulk;
177         }
178         r = usbd_interface_count(udev, &niface);
179         if (r) {
180                 ermsg = "iface";
181                 goto nobulk;
182         }
183         r = usbd_device2interface_handle(udev, 0, &iface);
184         if (r) {
185                 ermsg = "iface";
186                 goto nobulk;
187         }
188         sc->sc_iface = iface;
189 #endif
190         sc->sc_opened = 0;
191         sc->sc_refcnt = 0;
192
193         r = usbd_endpoint_count(iface, &epcount);
194         if (r != USBD_NORMAL_COMPLETION) {
195                 ermsg = "endpoints";
196                 goto nobulk;
197         }
198
199         edesc = usbd_interface2endpoint_descriptor(iface, 0);
200         if (!edesc) {
201                 ermsg = "interface endpoint";
202                 goto nobulk;
203         }
204         sc->sc_epaddr = edesc->bEndpointAddress;
205
206 #if defined(__FreeBSD__) || defined(__DragonFly__)
207         /* XXX no error trapping, no storing of cdev_t */
208         dev_ops_add(&ufm_ops, -1, device_get_unit(self));
209         make_dev(&ufm_ops, device_get_unit(self),
210                         UID_ROOT, GID_OPERATOR,
211                         0644, "ufm%d", device_get_unit(self));
212 #elif defined(__NetBSD__) || defined(__OpenBSD__)
213         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
214                            USBDEV(sc->sc_dev));
215 #endif
216
217         DPRINTFN(10, ("ufm_attach: %p\n", sc->sc_udev));
218
219         USB_ATTACH_SUCCESS_RETURN;
220
221  nobulk:
222         kprintf("%s: could not find %s\n", USBDEVNAME(sc->sc_dev),ermsg);
223         USB_ATTACH_ERROR_RETURN;
224 }
225
226
227 int
228 ufmopen(struct dev_open_args *ap)
229 {
230         cdev_t dev = ap->a_head.a_dev;
231         struct ufm_softc *sc;
232
233         int unit = UFMUNIT(dev);
234         USB_GET_SC_OPEN(ufm, unit, sc);
235
236         DPRINTFN(5, ("ufmopen: flag=%d, mode=%d, unit=%d\n",
237                      ap->a_oflags, ap->a_devtype, unit));
238
239         if (sc->sc_opened)
240                 return (EBUSY);
241
242         if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
243                 return (EACCES);
244
245         sc->sc_opened = 1;
246         return (0);
247 }
248
249 int
250 ufmclose(struct dev_close_args *ap)
251 {
252         cdev_t dev = ap->a_head.a_dev;
253         struct ufm_softc *sc;
254
255         int unit = UFMUNIT(dev);
256         USB_GET_SC(ufm, unit, sc);
257
258         DPRINTFN(5, ("ufmclose: flag=%d, mode=%d, unit=%d\n", 
259                     ap->a_fflag, ap->a_devtype, unit));
260         sc->sc_opened = 0;
261         sc->sc_refcnt = 0;
262         return 0;
263 }
264
265 static int
266 ufm_do_req(struct ufm_softc *sc, u_int8_t reqtype, u_int8_t request,
267     u_int16_t value, u_int16_t index, u_int8_t len, void *retbuf)
268 {
269         usb_device_request_t req;
270         usbd_status err;
271
272         crit_enter();
273         req.bmRequestType = reqtype;
274         req.bRequest = request;
275         USETW(req.wValue, value);
276         USETW(req.wIndex, index);
277         USETW(req.wLength, len);
278         err = usbd_do_request_flags(sc->sc_udev, &req, retbuf, 0, NULL,
279             USBD_DEFAULT_TIMEOUT);
280         crit_exit();
281         if (err) {
282                 kprintf("usbd_do_request_flags returned %#x\n", err);
283                 return (EIO);
284         }
285         return (0);
286 }
287
288 static int
289 ufm_set_freq(struct ufm_softc *sc, caddr_t addr)
290 {
291         int freq = *(int *)addr;
292         u_int8_t ret;
293
294         /*
295          * Freq now is in Hz.  We need to convert it to the frequency
296          * that the radio wants.  This frequency is 10.7MHz above
297          * the actual frequency.  We then need to convert to
298          * units of 12.5kHz.  We add one to the IFM to make rounding
299          * easier.
300          */
301         sc->sc_freq = freq;
302         freq = (freq + 10700001) / 12500;
303         /* This appears to set the frequency */
304         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD_SET_FREQ, freq >> 8,
305             freq, 1, &ret) != 0)
306                 return (EIO);
307         /* Not sure what this does */
308         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x96, 0xb7, 1,
309             &ret) != 0)
310                 return (EIO);
311         return (0);
312 }
313
314 static int
315 ufm_get_freq(struct ufm_softc *sc, caddr_t addr)
316 {
317         int *valp = (int *)addr;
318         *valp = sc->sc_freq;
319         return (0);
320 }
321
322 static int
323 ufm_start(struct ufm_softc *sc, caddr_t addr)
324 {
325         u_int8_t ret;
326
327         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0xc7,
328             1, &ret))
329                 return (EIO);
330         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x01, 0x00,
331             1, &ret))
332                 return (EIO);
333         if (ret & 0x1)
334                 return (EIO);
335         return (0);
336 }
337
338 static int
339 ufm_stop(struct ufm_softc *sc, caddr_t addr)
340 {
341         u_int8_t ret;
342
343         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x16, 0x1C,
344             1, &ret))
345                 return (EIO);
346         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x00, 0x00,
347             1, &ret))
348                 return (EIO);
349         return (0);
350 }
351
352 static int
353 ufm_get_stat(struct ufm_softc *sc, caddr_t addr)
354 {
355         u_int8_t ret;
356
357         /*
358          * Note, there's a 240ms settle time before the status
359          * will be valid, so tsleep that amount.  hz/4 is a good
360          * approximation of that.  Since this is a short sleep
361          * we don't try to catch any signals to keep things
362          * simple.
363          */
364         tsleep(sc, 0, "ufmwait", hz/4);
365         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0x24,
366             1, &ret))
367                 return (EIO);
368         *(int *)addr = ret;
369
370         return (0);
371 }
372
373 int
374 ufmioctl(struct dev_ioctl_args *ap)
375 {
376         cdev_t dev = ap->a_head.a_dev;
377         caddr_t addr = ap->a_data;
378         struct ufm_softc *sc;
379
380         int unit = UFMUNIT(dev);
381         int error = 0;
382
383         USB_GET_SC(ufm, unit, sc);
384
385         switch (ap->a_cmd) {
386         case FM_SET_FREQ:
387                 error = ufm_set_freq(sc, addr);
388                 break;
389         case FM_GET_FREQ:
390                 error = ufm_get_freq(sc, addr);
391                 break;
392         case FM_START:
393                 error = ufm_start(sc, addr);
394                 break;
395         case FM_STOP:
396                 error = ufm_stop(sc, addr);
397                 break;
398         case FM_GET_STAT:
399                 error = ufm_get_stat(sc, addr);
400                 break;
401         default:
402                 return ENOTTY;
403                 break;
404         }
405         return error;
406 }
407
408
409 #if defined(__NetBSD__) || defined(__OpenBSD__)
410 int
411 ufm_activate(device_ptr_t self, enum devact act)
412 {
413         struct ufm_softc *sc = (struct ufm_softc *)self;
414
415         switch (act) {
416         case DVACT_ACTIVATE:
417                 return (EOPNOTSUPP);
418                 break;
419
420         case DVACT_DEACTIVATE:
421                 sc->sc_dying = 1;
422                 break;
423         }
424         return (0);
425 }
426
427 USB_DETACH(ufm)
428 {
429         USB_DETACH_START(ufm, sc);
430         struct ufm_endpoint *sce;
431         int i, dir;
432 #if defined(__NetBSD__) || defined(__OpenBSD__)
433         int maj, mn;
434
435         DPRINTF(("ufm_detach: sc=%p flags=%d\n", sc, flags));
436 #elif defined(__FreeBSD__) || defined(__DragonFly__)
437         DPRINTF(("ufm_detach: sc=%p\n", sc));
438 #endif
439
440         sc->sc_dying = 1;
441
442         crit_enter();
443         if (--sc->sc_refcnt >= 0) {
444                 /* Wait for processes to go away. */
445                 usb_detach_wait(USBDEV(sc->sc_dev));
446         }
447         crit_exit();
448
449 #if defined(__NetBSD__) || defined(__OpenBSD__)
450         /* locate the major number */
451         for (maj = 0; maj < nchrdev; maj++)
452                 if (cdevsw[maj].d_open == ufmopen)
453                         break;
454
455         /* Nuke the vnodes for any open instances (calls close). */
456         mn = self->dv_unit * USB_MAX_ENDPOINTS;
457         vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
458 #elif defined(__FreeBSD__) || defined(__DragonFly__)
459         /* XXX not implemented yet */
460 #endif
461
462         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
463                            USBDEV(sc->sc_dev));
464
465         return (0);
466 }
467 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
468
469 #if defined(__FreeBSD__) || defined(__DragonFly__)
470 Static int
471 ufm_detach(device_t self)
472 {
473         DPRINTF(("%s: disconnected\n", USBDEVNAME(self)));
474         return 0;
475 }
476
477 DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, usbd_driver_load, 0);
478 #endif