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