]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/usb/ufm.c
This commit was generated by cvs2svn to compensate for changes in r163820,
[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 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #if defined(__NetBSD__)
40 #include <sys/device.h>
41 #include <sys/ioctl.h>
42 #elif defined(__FreeBSD__)
43 #include <sys/module.h>
44 #include <sys/bus.h>
45 #include <sys/ioccom.h>
46 #endif
47 #include <sys/fcntl.h>
48 #include <sys/filio.h>
49 #include <sys/conf.h>
50 #include <sys/uio.h>
51 #include <sys/tty.h>
52 #include <sys/file.h>
53 #if __FreeBSD_version >= 500014
54 #include <sys/selinfo.h>
55 #else
56 #include <sys/select.h>
57 #endif
58 #include <sys/poll.h>
59 #include <sys/sysctl.h>
60
61 #include <dev/usb/usb.h>
62 #include <dev/usb/usbdi.h>
63 #include <dev/usb/usbdi_util.h>
64
65 #include "usbdevs.h"
66 #include <dev/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 static struct cdevsw ufm_cdevsw = {
92         .d_version =    D_VERSION,
93         .d_flags =      D_NEEDGIANT,
94         .d_open =       ufmopen,
95         .d_close =      ufmclose,
96         .d_ioctl =      ufmioctl,
97         .d_name =       "ufm",
98 #if (__FreeBSD_version < 500014)
99         .d_bmaj =       -1
100 #endif
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         device_t 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
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 struct cdev **/
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", device_get_nameunit(sc->sc_dev),ermsg);
221         USB_ATTACH_ERROR_RETURN;
222 }
223
224
225 int
226 ufmopen(struct cdev *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(struct cdev *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             USBD_DEFAULT_TIMEOUT);
276         splx(s);
277         if (err) {
278                 printf("usbd_do_request_flags returned %#x\n", err);
279                 return (EIO);
280         }
281         return (0);
282 }
283
284 static int
285 ufm_set_freq(struct ufm_softc *sc, caddr_t addr)
286 {
287         int freq = *(int *)addr;
288         u_int8_t ret;
289
290         /*
291          * Freq now is in Hz.  We need to convert it to the frequency
292          * that the radio wants.  This frequency is 10.7MHz above
293          * the actual frequency.  We then need to convert to
294          * units of 12.5kHz.  We add one to the IFM to make rounding
295          * easier.
296          */
297         sc->sc_freq = freq;
298         freq = (freq + 10700001) / 12500;
299         /* This appears to set the frequency */
300         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD_SET_FREQ, freq >> 8,
301             freq, 1, &ret) != 0)
302                 return (EIO);
303         /* Not sure what this does */
304         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x96, 0xb7, 1,
305             &ret) != 0)
306                 return (EIO);
307         return (0);
308 }
309
310 static int
311 ufm_get_freq(struct ufm_softc *sc, caddr_t addr)
312 {
313         int *valp = (int *)addr;
314         *valp = sc->sc_freq;
315         return (0);
316 }
317
318 static int
319 ufm_start(struct ufm_softc *sc, caddr_t addr)
320 {
321         u_int8_t ret;
322
323         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0xc7,
324             1, &ret))
325                 return (EIO);
326         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x01, 0x00,
327             1, &ret))
328                 return (EIO);
329         if (ret & 0x1)
330                 return (EIO);
331         return (0);
332 }
333
334 static int
335 ufm_stop(struct ufm_softc *sc, caddr_t addr)
336 {
337         u_int8_t ret;
338
339         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x16, 0x1C,
340             1, &ret))
341                 return (EIO);
342         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x00, 0x00,
343             1, &ret))
344                 return (EIO);
345         return (0);
346 }
347
348 static int
349 ufm_get_stat(struct ufm_softc *sc, caddr_t addr)
350 {
351         u_int8_t ret;
352
353         /*
354          * Note, there's a 240ms settle time before the status
355          * will be valid, so tsleep that amount.  hz/4 is a good
356          * approximation of that.  Since this is a short sleep
357          * we don't try to catch any signals to keep things
358          * simple.
359          */
360         tsleep(sc, 0, "ufmwait", hz/4);
361         if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0x24,
362             1, &ret))
363                 return (EIO);
364         *(int *)addr = ret;
365
366         return (0);
367 }
368
369 int
370 ufmioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, usb_proc_ptr td)
371 {
372         struct ufm_softc *sc;
373
374         int unit = UFMUNIT(dev);
375         int error = 0;
376
377         USB_GET_SC(ufm, unit, sc);
378
379         switch (cmd) {
380         case FM_SET_FREQ:
381                 error = ufm_set_freq(sc, addr);
382                 break;
383         case FM_GET_FREQ:
384                 error = ufm_get_freq(sc, addr);
385                 break;
386         case FM_START:
387                 error = ufm_start(sc, addr);
388                 break;
389         case FM_STOP:
390                 error = ufm_stop(sc, addr);
391                 break;
392         case FM_GET_STAT:
393                 error = ufm_get_stat(sc, addr);
394                 break;
395         default:
396                 return ENOTTY;
397                 break;
398         }
399         return error;
400 }
401
402
403 #if defined(__NetBSD__) || defined(__OpenBSD__)
404 int
405 ufm_activate(device_t self, enum devact act)
406 {
407         struct ufm_softc *sc = (struct ufm_softc *)self;
408
409         switch (act) {
410         case DVACT_ACTIVATE:
411                 return (EOPNOTSUPP);
412                 break;
413
414         case DVACT_DEACTIVATE:
415                 sc->sc_dying = 1;
416                 break;
417         }
418         return (0);
419 }
420
421 USB_DETACH(ufm)
422 {
423         USB_DETACH_START(ufm, sc);
424         struct ufm_endpoint *sce;
425         int i, dir;
426         int s;
427 #if defined(__NetBSD__) || defined(__OpenBSD__)
428         int maj, mn;
429
430         DPRINTF(("ufm_detach: sc=%p flags=%d\n", sc, flags));
431 #elif defined(__FreeBSD__)
432         DPRINTF(("ufm_detach: sc=%p\n", sc));
433 #endif
434
435         sc->sc_dying = 1;
436
437         s = splusb();
438         if (--sc->sc_refcnt >= 0) {
439                 /* Wait for processes to go away. */
440                 usb_detach_wait(USBDEV(sc->sc_dev));
441         }
442         splx(s);
443
444 #if defined(__NetBSD__) || defined(__OpenBSD__)
445         /* locate the major number */
446         for (maj = 0; maj < nchrdev; maj++)
447                 if (cdevsw[maj].d_open == ufmopen)
448                         break;
449
450         /* Nuke the vnodes for any open instances (calls close). */
451         mn = self->dv_unit * USB_MAX_ENDPOINTS;
452         vdevgone(maj, mn, mn + USB_MAX_ENDPOINTS - 1, VCHR);
453 #elif defined(__FreeBSD__)
454         /* XXX not implemented yet */
455 #endif
456
457         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
458                            USBDEV(sc->sc_dev));
459
460         return (0);
461 }
462 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
463
464 #if defined(__FreeBSD__)
465 static int
466 ufm_detach(device_t self)
467 {
468         DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
469         return 0;
470 }
471
472 DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, usbd_driver_load, 0);
473 #endif