DEVFS - Bring in Alex's GSOC kernel adjustments.
[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 $
74781d8f 33 * $DragonFly: src/sys/dev/usbmisc/ufm/ufm.c,v 1.24 2008/08/14 20:55:53 hasso 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>
984263bc
MD
40#include <sys/module.h>
41#include <sys/bus.h>
984263bc
MD
42#include <sys/fcntl.h>
43#include <sys/filio.h>
44#include <sys/conf.h>
45#include <sys/uio.h>
46#include <sys/tty.h>
47#include <sys/file.h>
984263bc 48#include <sys/select.h>
984263bc
MD
49#include <sys/poll.h>
50#include <sys/sysctl.h>
4e01b467 51#include <sys/thread2.h>
984263bc 52
1f2de5d4
MD
53#include <bus/usb/usb.h>
54#include <bus/usb/usbdi.h>
55#include <bus/usb/usbdi_util.h>
984263bc 56
1f2de5d4 57#include <bus/usb/dsbr100io.h>
984263bc
MD
58
59#ifdef USB_DEBUG
fc1ef497
HT
60#define DPRINTF(x) if (ufmdebug) kprintf x
61#define DPRINTFN(n,x) if (ufmdebug>(n)) kprintf x
984263bc
MD
62int ufmdebug = 0;
63SYSCTL_NODE(_hw_usb, OID_AUTO, ufm, CTLFLAG_RW, 0, "USB ufm");
64SYSCTL_INT(_hw_usb_ufm, OID_AUTO, debug, CTLFLAG_RW,
65 &ufmdebug, 0, "ufm debug level");
66#else
67#define DPRINTF(x)
68#define DPRINTFN(n,x)
69#endif
70
984263bc
MD
71d_open_t ufmopen;
72d_close_t ufmclose;
73d_ioctl_t ufmioctl;
74
75#define UFM_CDEV_MAJOR 200
76
6ed427ca 77static struct dev_ops ufm_ops = {
fef8985e
MD
78 { "ufm", UFM_CDEV_MAJOR, 0 },
79 .d_open = ufmopen,
80 .d_close = ufmclose,
81 .d_ioctl = ufmioctl,
984263bc 82};
984263bc
MD
83
84#define FM_CMD0 0x00
85#define FM_CMD_SET_FREQ 0x01
86#define FM_CMD2 0x02
87
88struct ufm_softc {
6ed427ca 89 device_t sc_dev;
984263bc
MD
90 usbd_device_handle sc_udev;
91 usbd_interface_handle sc_iface;
92
93 int sc_opened;
94 int sc_epaddr;
95 int sc_freq;
96
97 int sc_refcnt;
984263bc
MD
98};
99
100#define UFMUNIT(n) (minor(n))
101
61b69189
HT
102static device_probe_t ufm_match;
103static device_attach_t ufm_attach;
104static device_detach_t ufm_detach;
105
106static devclass_t ufm_devclass;
107
108static kobj_method_t ufm_methods[] = {
109 DEVMETHOD(device_probe, ufm_match),
110 DEVMETHOD(device_attach, ufm_attach),
111 DEVMETHOD(device_detach, ufm_detach),
112 {0,0}
113};
114
115static driver_t ufm_driver = {
116 "ufm",
117 ufm_methods,
118 sizeof(struct ufm_softc)
119};
120
121MODULE_DEPEND(ufm, usb, 1, 1, 1);
984263bc 122
e785a5d9
HT
123static int
124ufm_match(device_t self)
984263bc 125{
e785a5d9 126 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc
MD
127 usb_device_descriptor_t *dd;
128
129 DPRINTFN(10,("ufm_match\n"));
130 if (!uaa->iface)
131 return UMATCH_NONE;
132
133 dd = usbd_get_device_descriptor(uaa->device);
134
135 if (dd &&
3f56796e 136 ((UGETW(dd->idVendor) == 0x04b4 && UGETW(dd->idProduct) == 0x1002)))
984263bc
MD
137 return UMATCH_VENDOR_PRODUCT;
138 else
139 return UMATCH_NONE;
140}
141
e785a5d9
HT
142static int
143ufm_attach(device_t self)
984263bc 144{
e785a5d9
HT
145 struct ufm_softc *sc = device_get_softc(self);
146 struct usb_attach_arg *uaa = device_get_ivars(self);
984263bc 147 usb_endpoint_descriptor_t *edesc;
984263bc
MD
148 usbd_interface_handle iface;
149 u_int8_t epcount;
984263bc
MD
150 usbd_status r;
151 char * ermsg = "<none>";
152
1550dfd9 153 DPRINTFN(10,("ufm_attach: sc=%p\n", sc));
e785a5d9 154 sc->sc_dev = self;
74781d8f 155 sc->sc_udev = uaa->device;
984263bc 156
984263bc
MD
157 if ((!uaa->device) || (!uaa->iface)) {
158 ermsg = "device or iface";
159 goto nobulk;
160 }
161 sc->sc_iface = iface = uaa->iface;
984263bc
MD
162 sc->sc_opened = 0;
163 sc->sc_refcnt = 0;
164
165 r = usbd_endpoint_count(iface, &epcount);
1550dfd9 166 if (r != USBD_NORMAL_COMPLETION) {
984263bc
MD
167 ermsg = "endpoints";
168 goto nobulk;
169 }
170
171 edesc = usbd_interface2endpoint_descriptor(iface, 0);
172 if (!edesc) {
173 ermsg = "interface endpoint";
174 goto nobulk;
175 }
176 sc->sc_epaddr = edesc->bEndpointAddress;
177
b13267a5 178 /* XXX no error trapping, no storing of cdev_t */
fef8985e
MD
179 dev_ops_add(&ufm_ops, -1, device_get_unit(self));
180 make_dev(&ufm_ops, device_get_unit(self),
984263bc
MD
181 UID_ROOT, GID_OPERATOR,
182 0644, "ufm%d", device_get_unit(self));
984263bc
MD
183
184 DPRINTFN(10, ("ufm_attach: %p\n", sc->sc_udev));
185
e785a5d9 186 return 0;
984263bc
MD
187
188 nobulk:
6ed427ca 189 kprintf("%s: could not find %s\n", device_get_nameunit(sc->sc_dev),ermsg);
e785a5d9 190 return ENXIO;
984263bc
MD
191}
192
193
194int
fef8985e 195ufmopen(struct dev_open_args *ap)
984263bc 196{
b13267a5 197 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
198 struct ufm_softc *sc;
199
200 int unit = UFMUNIT(dev);
1e089e7e
HT
201 sc = devclass_get_softc(ufm_devclass, unit);
202 if (sc == NULL)
203 return (ENXIO);
984263bc 204
1550dfd9 205 DPRINTFN(5, ("ufmopen: flag=%d, mode=%d, unit=%d\n",
fef8985e 206 ap->a_oflags, ap->a_devtype, unit));
984263bc
MD
207
208 if (sc->sc_opened)
209 return (EBUSY);
210
fef8985e 211 if ((ap->a_oflags & (FWRITE|FREAD)) != (FWRITE|FREAD))
984263bc
MD
212 return (EACCES);
213
214 sc->sc_opened = 1;
215 return (0);
216}
217
218int
fef8985e 219ufmclose(struct dev_close_args *ap)
984263bc 220{
b13267a5 221 cdev_t dev = ap->a_head.a_dev;
984263bc
MD
222 struct ufm_softc *sc;
223
224 int unit = UFMUNIT(dev);
1e089e7e 225 sc = devclass_get_softc(ufm_devclass, unit);
984263bc 226
fef8985e
MD
227 DPRINTFN(5, ("ufmclose: flag=%d, mode=%d, unit=%d\n",
228 ap->a_fflag, ap->a_devtype, unit));
984263bc
MD
229 sc->sc_opened = 0;
230 sc->sc_refcnt = 0;
1550dfd9 231 return 0;
984263bc
MD
232}
233
234static int
235ufm_do_req(struct ufm_softc *sc, u_int8_t reqtype, u_int8_t request,
236 u_int16_t value, u_int16_t index, u_int8_t len, void *retbuf)
237{
984263bc
MD
238 usb_device_request_t req;
239 usbd_status err;
240
4e01b467 241 crit_enter();
984263bc
MD
242 req.bmRequestType = reqtype;
243 req.bRequest = request;
244 USETW(req.wValue, value);
245 USETW(req.wIndex, index);
246 USETW(req.wLength, len);
1550dfd9
MD
247 err = usbd_do_request_flags(sc->sc_udev, &req, retbuf, 0, NULL,
248 USBD_DEFAULT_TIMEOUT);
4e01b467 249 crit_exit();
984263bc 250 if (err) {
e3869ec7 251 kprintf("usbd_do_request_flags returned %#x\n", err);
984263bc
MD
252 return (EIO);
253 }
254 return (0);
255}
256
257static int
258ufm_set_freq(struct ufm_softc *sc, caddr_t addr)
259{
260 int freq = *(int *)addr;
261 u_int8_t ret;
262
263 /*
264 * Freq now is in Hz. We need to convert it to the frequency
265 * that the radio wants. This frequency is 10.7MHz above
266 * the actual frequency. We then need to convert to
267 * units of 12.5kHz. We add one to the IFM to make rounding
1550dfd9 268 * easier.
984263bc
MD
269 */
270 sc->sc_freq = freq;
271 freq = (freq + 10700001) / 12500;
272 /* This appears to set the frequency */
273 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD_SET_FREQ, freq >> 8,
274 freq, 1, &ret) != 0)
275 return (EIO);
276 /* Not sure what this does */
1550dfd9 277 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x96, 0xb7, 1,
984263bc
MD
278 &ret) != 0)
279 return (EIO);
280 return (0);
281}
282
283static int
284ufm_get_freq(struct ufm_softc *sc, caddr_t addr)
285{
286 int *valp = (int *)addr;
287 *valp = sc->sc_freq;
288 return (0);
289}
290
291static int
292ufm_start(struct ufm_softc *sc, caddr_t addr)
293{
294 u_int8_t ret;
1550dfd9
MD
295
296 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0xc7,
984263bc
MD
297 1, &ret))
298 return (EIO);
299 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x01, 0x00,
300 1, &ret))
301 return (EIO);
302 if (ret & 0x1)
303 return (EIO);
304 return (0);
305}
306
307static int
308ufm_stop(struct ufm_softc *sc, caddr_t addr)
309{
310 u_int8_t ret;
1550dfd9 311
984263bc
MD
312 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x16, 0x1C,
313 1, &ret))
314 return (EIO);
315 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD2, 0x00, 0x00,
316 1, &ret))
317 return (EIO);
318 return (0);
319}
320
321static int
322ufm_get_stat(struct ufm_softc *sc, caddr_t addr)
323{
324 u_int8_t ret;
1550dfd9 325
984263bc
MD
326 /*
327 * Note, there's a 240ms settle time before the status
328 * will be valid, so tsleep that amount. hz/4 is a good
329 * approximation of that. Since this is a short sleep
330 * we don't try to catch any signals to keep things
331 * simple.
332 */
333 tsleep(sc, 0, "ufmwait", hz/4);
334 if (ufm_do_req(sc, UT_READ_VENDOR_DEVICE, FM_CMD0, 0x00, 0x24,
335 1, &ret))
336 return (EIO);
337 *(int *)addr = ret;
1550dfd9 338
984263bc
MD
339 return (0);
340}
341
342int
fef8985e 343ufmioctl(struct dev_ioctl_args *ap)
984263bc 344{
b13267a5 345 cdev_t dev = ap->a_head.a_dev;
fef8985e 346 caddr_t addr = ap->a_data;
984263bc
MD
347 struct ufm_softc *sc;
348
349 int unit = UFMUNIT(dev);
350 int error = 0;
351
1e089e7e 352 sc = devclass_get_softc(ufm_devclass, unit);
984263bc 353
fef8985e 354 switch (ap->a_cmd) {
984263bc
MD
355 case FM_SET_FREQ:
356 error = ufm_set_freq(sc, addr);
357 break;
358 case FM_GET_FREQ:
359 error = ufm_get_freq(sc, addr);
360 break;
361 case FM_START:
362 error = ufm_start(sc, addr);
363 break;
364 case FM_STOP:
365 error = ufm_stop(sc, addr);
366 break;
367 case FM_GET_STAT:
368 error = ufm_get_stat(sc, addr);
369 break;
370 default:
371 return ENOTTY;
372 break;
373 }
374 return error;
375}
376
6ed427ca 377static int
984263bc 378ufm_detach(device_t self)
1550dfd9 379{
6ed427ca 380 DPRINTF(("%s: disconnected\n", device_get_nameunit(self)));
984263bc
MD
381 return 0;
382}
383
384DRIVER_MODULE(ufm, uhub, ufm_driver, ufm_devclass, usbd_driver_load, 0);