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