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