Sync usage with man page.
[netbsd-mini2440.git] / sys / dev / usb / uirda.c
blob277d7b5afe7359be23ddfd4cf401285c147b1514
1 /* $NetBSD: uirda.c,v 1.32 2009/09/23 19:07:19 plunky Exp $ */
3 /*
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart@augustsson.net).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: uirda.c,v 1.32 2009/09/23 19:07:19 plunky Exp $");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/mutex.h>
40 #include <sys/ioctl.h>
41 #include <sys/conf.h>
42 #include <sys/file.h>
43 #include <sys/poll.h>
44 #include <sys/select.h>
45 #include <sys/proc.h>
47 #include <dev/usb/usb.h>
48 #include <dev/usb/usbdi.h>
49 #include <dev/usb/usbdi_util.h>
50 #include <dev/usb/usbdevs.h>
52 #include <dev/ir/ir.h>
53 #include <dev/ir/irdaio.h>
54 #include <dev/ir/irframevar.h>
56 #include <dev/usb/uirdavar.h>
58 #ifdef UIRDA_DEBUG
59 #define DPRINTF(x) if (uirdadebug) logprintf x
60 #define DPRINTFN(n,x) if (uirdadebug>(n)) logprintf x
61 int uirdadebug = 0;
62 #else
63 #define DPRINTF(x)
64 #define DPRINTFN(n,x)
65 #endif
68 /* Class specific requests */
69 #define UR_IRDA_RECEIVING 0x01 /* Receive in progress? */
70 #define UR_IRDA_CHECK_MEDIA_BUSY 0x03
71 #define UR_IRDA_SET_RATE_SNIFF 0x04 /* opt */
72 #define UR_IRDA_SET_UNICAST_LIST 0x05 /* opt */
73 #define UR_IRDA_GET_DESC 0x06
75 #define UIRDA_NEBOFS 8
76 static struct {
77 int count;
78 int mask;
79 int header;
80 } uirda_ebofs[UIRDA_NEBOFS] = {
81 { 0, UI_EB_0, UIRDA_EB_0 },
82 { 1, UI_EB_1, UIRDA_EB_1 },
83 { 2, UI_EB_2, UIRDA_EB_2 },
84 { 3, UI_EB_3, UIRDA_EB_3 },
85 { 6, UI_EB_6, UIRDA_EB_6 },
86 { 12, UI_EB_12, UIRDA_EB_12 },
87 { 24, UI_EB_24, UIRDA_EB_24 },
88 { 48, UI_EB_48, UIRDA_EB_48 }
91 #define UIRDA_NSPEEDS 9
92 static struct {
93 int speed;
94 int mask;
95 int header;
96 } uirda_speeds[UIRDA_NSPEEDS] = {
97 { 4000000, UI_BR_4000000, UIRDA_4000000 },
98 { 1152000, UI_BR_1152000, UIRDA_1152000 },
99 { 576000, UI_BR_576000, UIRDA_576000 },
100 { 115200, UI_BR_115200, UIRDA_115200 },
101 { 57600, UI_BR_57600, UIRDA_57600 },
102 { 38400, UI_BR_38400, UIRDA_38400 },
103 { 19200, UI_BR_19200, UIRDA_19200 },
104 { 9600, UI_BR_9600, UIRDA_9600 },
105 { 2400, UI_BR_2400, UIRDA_2400 },
110 int uirda_open(void *h, int flag, int mode, struct lwp *l);
111 int uirda_close(void *h, int flag, int mode, struct lwp *l);
112 int uirda_read(void *h, struct uio *uio, int flag);
113 int uirda_write(void *h, struct uio *uio, int flag);
114 int uirda_set_params(void *h, struct irda_params *params);
115 int uirda_get_speeds(void *h, int *speeds);
116 int uirda_get_turnarounds(void *h, int *times);
117 int uirda_poll(void *h, int events, struct lwp *l);
118 int uirda_kqfilter(void *h, struct knote *kn);
120 struct irframe_methods uirda_methods = {
121 uirda_open, uirda_close, uirda_read, uirda_write, uirda_poll,
122 uirda_kqfilter, uirda_set_params, uirda_get_speeds,
123 uirda_get_turnarounds
126 void uirda_rd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
127 usbd_status status);
128 usbd_status uirda_start_read(struct uirda_softc *sc);
131 * These devices don't quite follow the spec. Speed changing is broken
132 * and they don't handle windows.
133 * But we change speed in a safe way, and don't use windows now.
134 * Some devices also seem to have an interrupt pipe that can be ignored.
136 * Table information taken from Linux driver.
138 Static const struct usb_devno uirda_devs[] = {
139 { USB_VENDOR_ACTISYS, USB_PRODUCT_ACTISYS_IR2000U },
140 { USB_VENDOR_EXTENDED, USB_PRODUCT_EXTENDED_XTNDACCESS },
141 { USB_VENDOR_KAWATSU, USB_PRODUCT_KAWATSU_KC180 },
143 #define uirda_lookup(v, p) (usb_lookup(uirda_devs, v, p))
145 int uirda_match(device_t, cfdata_t, void *);
146 void uirda_attach(device_t, device_t, void *);
147 void uirda_childdet(device_t, device_t);
148 int uirda_detach(device_t, int);
149 int uirda_activate(device_t, enum devact);
150 extern struct cfdriver uirda_cd;
151 CFATTACH_DECL2_NEW(uirda, sizeof(struct uirda_softc), uirda_match,
152 uirda_attach, uirda_detach, uirda_activate, NULL, uirda_childdet);
154 USB_MATCH(uirda)
156 USB_IFMATCH_START(uirda, uaa);
158 DPRINTFN(50,("uirda_match\n"));
160 if (uirda_lookup(uaa->vendor, uaa->product) != NULL)
161 return (UMATCH_VENDOR_PRODUCT);
163 if (uaa->class == UICLASS_APPL_SPEC &&
164 uaa->subclass == UISUBCLASS_IRDA &&
165 uaa->proto == UIPROTO_IRDA)
166 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
167 return (UMATCH_NONE);
170 USB_ATTACH(uirda)
172 USB_IFATTACH_START(uirda, sc, uaa);
173 usbd_device_handle dev = uaa->device;
174 usbd_interface_handle iface = uaa->iface;
175 char *devinfop;
176 usb_endpoint_descriptor_t *ed;
177 usbd_status err;
178 u_int8_t epcount;
179 u_int specrev;
180 int i;
181 struct ir_attach_args ia;
183 DPRINTFN(10,("uirda_attach: sc=%p\n", sc));
185 sc->sc_dev = self;
187 aprint_naive("\n");
188 aprint_normal("\n");
190 devinfop = usbd_devinfo_alloc(dev, 0);
191 aprint_normal_dev(self, "%s\n", devinfop);
192 usbd_devinfo_free(devinfop);
194 sc->sc_udev = dev;
195 sc->sc_iface = iface;
197 if (sc->sc_hdszi == 0)
198 sc->sc_hdszi = UIRDA_INPUT_HEADER_SIZE;
200 epcount = 0;
201 (void)usbd_endpoint_count(iface, &epcount);
203 sc->sc_rd_addr = -1;
204 sc->sc_wr_addr = -1;
205 for (i = 0; i < epcount; i++) {
206 ed = usbd_interface2endpoint_descriptor(iface, i);
207 if (ed == NULL) {
208 aprint_error_dev(self, "couldn't get ep %d\n", i);
209 USB_ATTACH_ERROR_RETURN;
211 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
212 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
213 sc->sc_rd_addr = ed->bEndpointAddress;
214 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
215 UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK) {
216 sc->sc_wr_addr = ed->bEndpointAddress;
219 if (sc->sc_rd_addr == -1 || sc->sc_wr_addr == -1) {
220 aprint_error_dev(self, "missing endpoint\n");
221 USB_ATTACH_ERROR_RETURN;
224 if (sc->sc_loadfw(sc) != 0) {
225 USB_ATTACH_ERROR_RETURN;
228 /* Get the IrDA descriptor */
229 err = usbd_get_class_desc(sc->sc_udev, UDESC_IRDA, 0,
230 USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
231 aprint_error_dev(self, "error %d reading class desc\n", err);
232 if (err) {
233 err = usbd_get_desc(sc->sc_udev, UDESC_IRDA, 0,
234 USB_IRDA_DESCRIPTOR_SIZE, &sc->sc_irdadesc);
236 aprint_error_dev(self, "error %d reading desc\n", err);
237 if (err) {
238 /* maybe it's embedded in the config desc? */
239 usbd_desc_iter_t iter;
240 const usb_descriptor_t *d;
241 usb_desc_iter_init(sc->sc_udev, &iter);
242 for (;;) {
243 d = usb_desc_iter_next(&iter);
244 if (!d || d->bDescriptorType == UDESC_IRDA)
245 break;
247 if (d == NULL) {
248 aprint_error_dev(self,
249 "Cannot get IrDA descriptor\n");
250 USB_ATTACH_ERROR_RETURN;
252 memcpy(&sc->sc_irdadesc, d, USB_IRDA_DESCRIPTOR_SIZE);
254 DPRINTF(("uirda_attach: bDescriptorSize %d bDescriptorType 0x%x "
255 "bmDataSize=0x%02x bmWindowSize=0x%02x "
256 "bmMinTurnaroundTime=0x%02x wBaudRate=0x%04x "
257 "bmAdditionalBOFs=0x%02x bIrdaSniff=%d bMaxUnicastList=%d\n",
258 sc->sc_irdadesc.bLength,
259 sc->sc_irdadesc.bDescriptorType,
260 sc->sc_irdadesc.bmDataSize,
261 sc->sc_irdadesc.bmWindowSize,
262 sc->sc_irdadesc.bmMinTurnaroundTime,
263 UGETW(sc->sc_irdadesc.wBaudRate),
264 sc->sc_irdadesc.bmAdditionalBOFs,
265 sc->sc_irdadesc.bIrdaSniff,
266 sc->sc_irdadesc.bMaxUnicastList));
268 specrev = UGETW(sc->sc_irdadesc.bcdSpecRevision);
269 aprint_normal_dev(self, "USB-IrDA protocol version %x.%02x\n",
270 specrev >> 8, specrev & 0xff);
272 DPRINTFN(10, ("uirda_attach: %p\n", sc->sc_udev));
274 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
275 USBDEV(sc->sc_dev));
277 mutex_init(&sc->sc_wr_buf_lk, MUTEX_DEFAULT, IPL_NONE);
278 mutex_init(&sc->sc_rd_buf_lk, MUTEX_DEFAULT, IPL_NONE);
279 selinit(&sc->sc_rd_sel);
280 selinit(&sc->sc_wr_sel);
282 ia.ia_type = IR_TYPE_IRFRAME;
283 ia.ia_methods = sc->sc_irm ? sc->sc_irm : &uirda_methods;
284 ia.ia_handle = sc;
286 sc->sc_child = config_found(self, &ia, ir_print);
288 USB_ATTACH_SUCCESS_RETURN;
291 USB_DETACH(uirda)
293 USB_DETACH_START(uirda, sc);
294 int s;
295 int rv = 0;
297 DPRINTF(("uirda_detach: sc=%p flags=%d\n", sc, flags));
299 sc->sc_dying = 1;
300 /* Abort all pipes. Causes processes waiting for transfer to wake. */
301 if (sc->sc_rd_pipe != NULL) {
302 usbd_abort_pipe(sc->sc_rd_pipe);
303 usbd_close_pipe(sc->sc_rd_pipe);
304 sc->sc_rd_pipe = NULL;
306 if (sc->sc_wr_pipe != NULL) {
307 usbd_abort_pipe(sc->sc_wr_pipe);
308 usbd_close_pipe(sc->sc_wr_pipe);
309 sc->sc_wr_pipe = NULL;
311 wakeup(&sc->sc_rd_count);
313 s = splusb();
314 if (--sc->sc_refcnt >= 0) {
315 /* Wait for processes to go away. */
316 usb_detach_wait(USBDEV(sc->sc_dev));
318 splx(s);
320 if (sc->sc_child != NULL)
321 rv = config_detach(sc->sc_child, flags);
323 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
324 USBDEV(sc->sc_dev));
326 mutex_destroy(&sc->sc_wr_buf_lk);
327 mutex_destroy(&sc->sc_rd_buf_lk);
328 seldestroy(&sc->sc_rd_sel);
329 seldestroy(&sc->sc_wr_sel);
331 return (rv);
334 void
335 uirda_childdet(device_t self, device_t child)
337 struct uirda_softc *sc = device_private(self);
339 KASSERT(sc->sc_child == child);
340 sc->sc_child = NULL;
344 uirda_activate(device_t self, enum devact act)
346 struct uirda_softc *sc = device_private(self);
348 switch (act) {
349 case DVACT_DEACTIVATE:
350 sc->sc_dying = 1;
351 return 0;
352 default:
353 return EOPNOTSUPP;
358 uirda_open(void *h, int flag, int mode,
359 struct lwp *l)
361 struct uirda_softc *sc = h;
362 int error;
363 usbd_status err;
365 DPRINTF(("%s: sc=%p\n", __func__, sc));
367 err = usbd_open_pipe(sc->sc_iface, sc->sc_rd_addr, 0, &sc->sc_rd_pipe);
368 if (err) {
369 error = EIO;
370 goto bad1;
372 err = usbd_open_pipe(sc->sc_iface, sc->sc_wr_addr, 0, &sc->sc_wr_pipe);
373 if (err) {
374 error = EIO;
375 goto bad2;
377 sc->sc_rd_xfer = usbd_alloc_xfer(sc->sc_udev);
378 if (sc->sc_rd_xfer == NULL) {
379 error = ENOMEM;
380 goto bad3;
382 sc->sc_wr_xfer = usbd_alloc_xfer(sc->sc_udev);
383 if (sc->sc_wr_xfer == NULL) {
384 error = ENOMEM;
385 goto bad4;
387 sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer,
388 IRDA_MAX_FRAME_SIZE + sc->sc_hdszi);
389 if (sc->sc_rd_buf == NULL) {
390 error = ENOMEM;
391 goto bad5;
393 sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer,
394 IRDA_MAX_FRAME_SIZE + UIRDA_OUTPUT_HEADER_SIZE +
395 2 + 1 /* worst case ST-UIRDA */);
396 if (sc->sc_wr_buf == NULL) {
397 error = ENOMEM;
398 goto bad5;
400 sc->sc_rd_count = 0;
401 sc->sc_rd_err = 0;
402 sc->sc_params.speed = 0;
403 sc->sc_params.ebofs = 0;
404 sc->sc_params.maxsize = IRDA_MAX_FRAME_SIZE;
405 sc->sc_wr_hdr = -1;
407 err = uirda_start_read(sc);
408 /* XXX check err */
410 return (0);
412 bad5:
413 usbd_free_xfer(sc->sc_wr_xfer);
414 sc->sc_wr_xfer = NULL;
415 bad4:
416 usbd_free_xfer(sc->sc_rd_xfer);
417 sc->sc_rd_xfer = NULL;
418 bad3:
419 usbd_close_pipe(sc->sc_wr_pipe);
420 sc->sc_wr_pipe = NULL;
421 bad2:
422 usbd_close_pipe(sc->sc_rd_pipe);
423 sc->sc_rd_pipe = NULL;
424 bad1:
425 return (error);
429 uirda_close(void *h, int flag, int mode,
430 struct lwp *l)
432 struct uirda_softc *sc = h;
434 DPRINTF(("%s: sc=%p\n", __func__, sc));
436 if (sc->sc_rd_pipe != NULL) {
437 usbd_abort_pipe(sc->sc_rd_pipe);
438 usbd_close_pipe(sc->sc_rd_pipe);
439 sc->sc_rd_pipe = NULL;
441 if (sc->sc_wr_pipe != NULL) {
442 usbd_abort_pipe(sc->sc_wr_pipe);
443 usbd_close_pipe(sc->sc_wr_pipe);
444 sc->sc_wr_pipe = NULL;
446 if (sc->sc_rd_xfer != NULL) {
447 usbd_free_xfer(sc->sc_rd_xfer);
448 sc->sc_rd_xfer = NULL;
449 sc->sc_rd_buf = NULL;
451 if (sc->sc_wr_xfer != NULL) {
452 usbd_free_xfer(sc->sc_wr_xfer);
453 sc->sc_wr_xfer = NULL;
454 sc->sc_wr_buf = NULL;
457 return (0);
461 uirda_read(void *h, struct uio *uio, int flag)
463 struct uirda_softc *sc = h;
464 usbd_status err;
465 int s;
466 int error;
467 u_int n;
469 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
471 if (sc->sc_dying)
472 return (EIO);
474 #ifdef DIAGNOSTIC
475 if (sc->sc_rd_buf == NULL)
476 return (EINVAL);
477 #endif
479 sc->sc_refcnt++;
481 do {
482 s = splusb();
483 while (sc->sc_rd_count == 0) {
484 DPRINTFN(5,("uirda_read: calling tsleep()\n"));
485 error = tsleep(&sc->sc_rd_count, PZERO | PCATCH,
486 "uirdrd", 0);
487 if (sc->sc_dying)
488 error = EIO;
489 if (error) {
490 splx(s);
491 DPRINTF(("uirda_read: tsleep() = %d\n", error));
492 goto ret;
495 splx(s);
497 mutex_enter(&sc->sc_rd_buf_lk);
498 n = sc->sc_rd_count - sc->sc_hdszi;
499 DPRINTFN(1,("%s: sc=%p n=%u, hdr=0x%02x\n", __func__,
500 sc, n, sc->sc_rd_buf[0]));
501 if (n > uio->uio_resid)
502 error = EINVAL;
503 else
504 error = uiomove(sc->sc_rd_buf + sc->sc_hdszi, n, uio);
505 sc->sc_rd_count = 0;
506 mutex_exit(&sc->sc_rd_buf_lk);
508 err = uirda_start_read(sc);
509 /* XXX check err */
511 } while (n == 0);
513 DPRINTFN(1,("uirda_read: return %d\n", error));
515 ret:
516 if (--sc->sc_refcnt < 0)
517 usb_detach_wakeup(USBDEV(sc->sc_dev));
518 return (error);
522 uirda_write(void *h, struct uio *uio, int flag)
524 struct uirda_softc *sc = h;
525 usbd_status err;
526 u_int32_t n;
527 int error = 0;
529 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
531 if (sc->sc_dying)
532 return (EIO);
534 #ifdef DIAGNOSTIC
535 if (sc->sc_wr_buf == NULL)
536 return (EINVAL);
537 #endif
539 n = uio->uio_resid;
540 if (n > sc->sc_params.maxsize)
541 return (EINVAL);
543 sc->sc_refcnt++;
544 mutex_enter(&sc->sc_wr_buf_lk);
546 sc->sc_wr_buf[0] = UIRDA_EB_NO_CHANGE | UIRDA_NO_SPEED;
547 error = uiomove(sc->sc_wr_buf + UIRDA_OUTPUT_HEADER_SIZE, n, uio);
548 if (!error) {
549 DPRINTFN(1, ("uirdawrite: transfer %d bytes\n", n));
551 n += UIRDA_OUTPUT_HEADER_SIZE;
552 err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
553 USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
554 UIRDA_WR_TIMEOUT,
555 sc->sc_wr_buf, &n, "uirdawr");
556 DPRINTFN(2, ("uirdawrite: err=%d\n", err));
557 if (err) {
558 if (err == USBD_INTERRUPTED)
559 error = EINTR;
560 else if (err == USBD_TIMEOUT)
561 error = ETIMEDOUT;
562 else
563 error = EIO;
567 mutex_exit(&sc->sc_wr_buf_lk);
568 if (--sc->sc_refcnt < 0)
569 usb_detach_wakeup(USBDEV(sc->sc_dev));
571 DPRINTFN(1,("%s: sc=%p done\n", __func__, sc));
572 return (error);
576 uirda_poll(void *h, int events, struct lwp *l)
578 struct uirda_softc *sc = h;
579 int revents = 0;
580 int s;
582 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
584 s = splusb();
585 if (events & (POLLOUT | POLLWRNORM))
586 revents |= events & (POLLOUT | POLLWRNORM);
587 if (events & (POLLIN | POLLRDNORM)) {
588 if (sc->sc_rd_count != 0) {
589 DPRINTFN(2,("%s: have data\n", __func__));
590 revents |= events & (POLLIN | POLLRDNORM);
591 } else {
592 DPRINTFN(2,("%s: recording select\n", __func__));
593 selrecord(l, &sc->sc_rd_sel);
596 splx(s);
598 return (revents);
601 static void
602 filt_uirdardetach(struct knote *kn)
604 struct uirda_softc *sc = kn->kn_hook;
605 int s;
607 s = splusb();
608 SLIST_REMOVE(&sc->sc_rd_sel.sel_klist, kn, knote, kn_selnext);
609 splx(s);
612 static int
613 filt_uirdaread(struct knote *kn, long hint)
615 struct uirda_softc *sc = kn->kn_hook;
617 kn->kn_data = sc->sc_rd_count;
618 return (kn->kn_data > 0);
621 static void
622 filt_uirdawdetach(struct knote *kn)
624 struct uirda_softc *sc = kn->kn_hook;
625 int s;
627 s = splusb();
628 SLIST_REMOVE(&sc->sc_wr_sel.sel_klist, kn, knote, kn_selnext);
629 splx(s);
632 static const struct filterops uirdaread_filtops =
633 { 1, NULL, filt_uirdardetach, filt_uirdaread };
634 static const struct filterops uirdawrite_filtops =
635 { 1, NULL, filt_uirdawdetach, filt_seltrue };
638 uirda_kqfilter(void *h, struct knote *kn)
640 struct uirda_softc *sc = kn->kn_hook;
641 struct klist *klist;
642 int s;
644 switch (kn->kn_filter) {
645 case EVFILT_READ:
646 klist = &sc->sc_rd_sel.sel_klist;
647 kn->kn_fop = &uirdaread_filtops;
648 break;
649 case EVFILT_WRITE:
650 klist = &sc->sc_wr_sel.sel_klist;
651 kn->kn_fop = &uirdawrite_filtops;
652 break;
653 default:
654 return (EINVAL);
657 kn->kn_hook = sc;
659 s = splusb();
660 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
661 splx(s);
663 return (0);
667 uirda_set_params(void *h, struct irda_params *p)
669 struct uirda_softc *sc = h;
670 usbd_status err;
671 int i;
672 u_int8_t hdr;
673 u_int32_t n;
674 u_int mask;
676 DPRINTF(("%s: sc=%p, speed=%d ebofs=%d maxsize=%d\n", __func__,
677 sc, p->speed, p->ebofs, p->maxsize));
679 if (sc->sc_dying)
680 return (EIO);
682 hdr = 0;
683 if (p->ebofs != sc->sc_params.ebofs) {
684 /* round up ebofs */
685 mask = 1 /* sc->sc_irdadesc.bmAdditionalBOFs*/;
686 DPRINTF(("u.s.p.: mask=0x%x, sc->ebofs=%d, p->ebofs=%d\n",
687 mask, sc->sc_params.ebofs, p->ebofs));
688 for (i = 0; i < UIRDA_NEBOFS; i++) {
689 DPRINTF(("u.s.p.: u_e[%d].mask=0x%x, count=%d\n",
690 i, uirda_ebofs[i].mask, uirda_ebofs[i].count));
691 if ((mask & uirda_ebofs[i].mask) &&
692 uirda_ebofs[i].count >= p->ebofs) {
693 hdr = uirda_ebofs[i].header;
694 goto found1;
697 for (i = 0; i < UIRDA_NEBOFS; i++) {
698 DPRINTF(("u.s.p.: u_e[%d].mask=0x%x, count=%d\n",
699 i, uirda_ebofs[i].mask, uirda_ebofs[i].count));
700 if ((mask & uirda_ebofs[i].mask)) {
701 hdr = uirda_ebofs[i].header;
702 goto found1;
705 /* no good value found */
706 return (EINVAL);
707 found1:
708 DPRINTF(("uirda_set_params: ebofs hdr=0x%02x\n", hdr));
712 if (hdr != 0 || p->speed != sc->sc_params.speed) {
713 /* find speed */
714 mask = UGETW(sc->sc_irdadesc.wBaudRate);
715 for (i = 0; i < UIRDA_NSPEEDS; i++) {
716 if ((mask & uirda_speeds[i].mask) &&
717 uirda_speeds[i].speed == p->speed) {
718 hdr |= uirda_speeds[i].header;
719 goto found2;
722 /* no good value found */
723 return (EINVAL);
724 found2:
725 DPRINTF(("uirda_set_params: speed hdr=0x%02x\n", hdr));
728 if (p->maxsize != sc->sc_params.maxsize) {
729 if (p->maxsize > IRDA_MAX_FRAME_SIZE)
730 return (EINVAL);
731 sc->sc_params.maxsize = p->maxsize;
732 #if 0
733 DPRINTF(("%s: new buffers, old size=%d\n", __func__,
734 sc->sc_params.maxsize));
735 if (p->maxsize > 10000 || p < 0) /* XXX */
736 return (EINVAL);
738 /* Change the write buffer */
739 mutex_enter(&sc->sc_wr_buf_lk);
740 if (sc->sc_wr_buf != NULL)
741 usbd_free_buffer(sc->sc_wr_xfer);
742 sc->sc_wr_buf = usbd_alloc_buffer(sc->sc_wr_xfer, p->maxsize+1);
743 mutex_exit(&sc->sc_wr_buf_lk);
744 if (sc->sc_wr_buf == NULL)
745 return (ENOMEM);
747 /* Change the read buffer */
748 mutex_enter(&sc->sc_rd_buf_lk);
749 usbd_abort_pipe(sc->sc_rd_pipe);
750 if (sc->sc_rd_buf != NULL)
751 usbd_free_buffer(sc->sc_rd_xfer);
752 sc->sc_rd_buf = usbd_alloc_buffer(sc->sc_rd_xfer, p->maxsize+1);
753 sc->sc_rd_count = 0;
754 if (sc->sc_rd_buf == NULL) {
755 mutex_exit(&sc->sc_rd_buf_lk);
756 return (ENOMEM);
758 sc->sc_params.maxsize = p->maxsize;
759 err = uirda_start_read(sc); /* XXX check */
760 mutex_exit(&sc->sc_rd_buf_lk);
761 #endif
763 if (hdr != 0 && hdr != sc->sc_wr_hdr) {
765 * A change has occurred, transmit a 0 length frame with
766 * the new settings. The 0 length frame is not sent to the
767 * device.
769 DPRINTF(("%s: sc=%p setting header 0x%02x\n",
770 __func__, sc, hdr));
771 sc->sc_wr_hdr = hdr;
772 mutex_enter(&sc->sc_wr_buf_lk);
773 sc->sc_wr_buf[0] = hdr;
774 n = UIRDA_OUTPUT_HEADER_SIZE;
775 err = usbd_bulk_transfer(sc->sc_wr_xfer, sc->sc_wr_pipe,
776 USBD_FORCE_SHORT_XFER | USBD_NO_COPY,
777 UIRDA_WR_TIMEOUT, sc->sc_wr_buf, &n, "uirdast");
778 if (err) {
779 aprint_error_dev(sc->sc_dev, "set failed, err=%d\n",
780 err);
781 usbd_clear_endpoint_stall(sc->sc_wr_pipe);
783 mutex_exit(&sc->sc_wr_buf_lk);
786 sc->sc_params = *p;
788 return (0);
792 uirda_get_speeds(void *h, int *speeds)
794 struct uirda_softc *sc = h;
795 u_int isp;
796 u_int usp;
798 DPRINTF(("%s: sc=%p\n", __func__, sc));
800 if (sc->sc_dying)
801 return (EIO);
803 usp = UGETW(sc->sc_irdadesc.wBaudRate);
804 isp = 0;
805 if (usp & UI_BR_4000000) isp |= IRDA_SPEED_4000000;
806 if (usp & UI_BR_1152000) isp |= IRDA_SPEED_1152000;
807 if (usp & UI_BR_576000) isp |= IRDA_SPEED_576000;
808 if (usp & UI_BR_115200) isp |= IRDA_SPEED_115200;
809 if (usp & UI_BR_57600) isp |= IRDA_SPEED_57600;
810 if (usp & UI_BR_38400) isp |= IRDA_SPEED_38400;
811 if (usp & UI_BR_19200) isp |= IRDA_SPEED_19200;
812 if (usp & UI_BR_9600) isp |= IRDA_SPEED_9600;
813 if (usp & UI_BR_2400) isp |= IRDA_SPEED_2400;
814 *speeds = isp;
815 DPRINTF(("%s: speeds = 0x%x\n", __func__, isp));
816 return (0);
820 uirda_get_turnarounds(void *h, int *turnarounds)
822 struct uirda_softc *sc = h;
823 u_int ita;
824 u_int uta;
826 DPRINTF(("%s: sc=%p\n", __func__, sc));
828 if (sc->sc_dying)
829 return (EIO);
831 uta = sc->sc_irdadesc.bmMinTurnaroundTime;
832 ita = 0;
833 if (uta & UI_TA_0) ita |= IRDA_TURNT_0;
834 if (uta & UI_TA_10) ita |= IRDA_TURNT_10;
835 if (uta & UI_TA_50) ita |= IRDA_TURNT_50;
836 if (uta & UI_TA_100) ita |= IRDA_TURNT_100;
837 if (uta & UI_TA_500) ita |= IRDA_TURNT_500;
838 if (uta & UI_TA_1000) ita |= IRDA_TURNT_1000;
839 if (uta & UI_TA_5000) ita |= IRDA_TURNT_5000;
840 if (uta & UI_TA_10000) ita |= IRDA_TURNT_10000;
841 *turnarounds = ita;
842 return (0);
845 void
846 uirda_rd_cb(usbd_xfer_handle xfer, usbd_private_handle priv,
847 usbd_status status)
849 struct uirda_softc *sc = priv;
850 u_int32_t size;
852 DPRINTFN(1,("%s: sc=%p\n", __func__, sc));
854 if (status == USBD_CANCELLED) /* this is normal */
855 return;
856 if (status) {
857 size = sc->sc_hdszi;
858 sc->sc_rd_err = 1;
859 } else {
860 usbd_get_xfer_status(xfer, NULL, NULL, &size, NULL);
862 DPRINTFN(1,("%s: sc=%p size=%u, err=%d\n", __func__, sc, size,
863 sc->sc_rd_err));
864 sc->sc_rd_count = size;
865 wakeup(&sc->sc_rd_count); /* XXX should use flag */
866 selnotify(&sc->sc_rd_sel, 0, 0);
869 usbd_status
870 uirda_start_read(struct uirda_softc *sc)
872 usbd_status err;
874 DPRINTFN(1,("%s: sc=%p, size=%d\n", __func__, sc,
875 sc->sc_params.maxsize + UIRDA_INPUT_HEADER_SIZE));
877 if (sc->sc_dying)
878 return (USBD_IOERROR);
880 if (sc->sc_rd_err) {
881 sc->sc_rd_err = 0;
882 DPRINTF(("uirda_start_read: clear stall\n"));
883 usbd_clear_endpoint_stall(sc->sc_rd_pipe);
886 usbd_setup_xfer(sc->sc_rd_xfer, sc->sc_rd_pipe, sc, sc->sc_rd_buf,
887 sc->sc_params.maxsize + sc->sc_hdszi,
888 USBD_SHORT_XFER_OK | USBD_NO_COPY,
889 USBD_NO_TIMEOUT, uirda_rd_cb);
890 err = usbd_transfer(sc->sc_rd_xfer);
891 if (err != USBD_IN_PROGRESS) {
892 DPRINTF(("uirda_start_read: err=%d\n", err));
893 return (err);
895 return (USBD_NORMAL_COMPLETION);
898 usbd_status
899 usbd_get_class_desc(usbd_device_handle dev, int type, int index, int len, void *desc)
901 usb_device_request_t req;
903 DPRINTFN(3,("usbd_get_desc: type=%d, index=%d, len=%d\n",
904 type, index, len));
906 req.bmRequestType = 0xa1; /* XXX ? */
907 req.bRequest = UR_GET_DESCRIPTOR;
908 USETW2(req.wValue, type, index);
909 USETW(req.wIndex, 0);
910 USETW(req.wLength, len);
911 return (usbd_do_request(dev, &req, desc));