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