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