Home | History | Annotate | Line # | Download | only in pci
oboe.c revision 1.39
      1 /*	$NetBSD: oboe.c,v 1.39 2012/10/27 17:18:35 chs Exp $	*/
      2 
      3 /*	XXXXFVDL THIS DRIVER IS BROKEN FOR NON-i386 -- vtophys() usage	*/
      4 
      5 /*-
      6  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      7  * All rights reserved.
      8  *
      9  * This code is derived from software contributed to The NetBSD Foundation
     10  * by Jan Sparud.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Toshiba OBOE IrDA SIR/FIR driver.
     36  *
     37  * Based on information from the Linux driver, thus the magic hex numbers.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: oboe.c,v 1.39 2012/10/27 17:18:35 chs Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.h>
     47 #include <sys/malloc.h>
     48 #include <sys/tty.h>
     49 #include <sys/vnode.h>
     50 #include <sys/poll.h>
     51 #include <sys/proc.h>
     52 
     53 #include <dev/ir/ir.h>
     54 #include <dev/ir/irdaio.h>
     55 #include <dev/ir/irframevar.h>
     56 #include <dev/ir/sir.h>
     57 
     58 #include <dev/pci/pcidevs.h>
     59 #include <dev/pci/pcivar.h>
     60 
     61 #include <sys/bus.h>
     62 #include <sys/intr.h>
     63 
     64 #include <dev/pci/oboereg.h>
     65 
     66 static int oboe_match(device_t parent, cfdata_t match, void *aux);
     67 static void oboe_attach(device_t parent, device_t self, void *aux);
     68 static int oboe_detach(device_t self, int flags);
     69 
     70 static int oboe_open(void *h, int flag, int mode, struct lwp *l);
     71 static int oboe_close(void *h, int flag, int mode, struct lwp *l);
     72 static int oboe_read(void *h, struct uio *uio, int flag);
     73 static int oboe_write(void *h, struct uio *uio, int flag);
     74 static int oboe_set_params(void *h, struct irda_params *params);
     75 static int oboe_get_speeds(void *h, int *speeds);
     76 static int oboe_get_turnarounds(void *h, int *times);
     77 static int oboe_poll(void *h, int events, struct lwp *l);
     78 static int oboe_kqfilter(void *h, struct knote *kn);
     79 
     80 #ifdef OBOE_DEBUG
     81 #define DPRINTF(x)	if (oboedebug) printf x
     82 int oboedebug = 1;
     83 #else
     84 #define DPRINTF(x)
     85 #endif
     86 
     87 struct oboe_dma;
     88 
     89 struct oboe_softc {
     90 	device_t		sc_child;
     91 	struct pci_attach_args	sc_pa;
     92 	pci_intr_handle_t *	sc_ih;
     93 	unsigned int		sc_revision;	/* PCI Revision ID */
     94 	/* I/O Base device */
     95 	bus_space_tag_t		sc_iot;
     96 	bus_space_handle_t	sc_ioh;
     97 	bus_dma_tag_t		sc_dmatag;
     98 	struct selinfo		sc_rsel;
     99 	struct selinfo		sc_wsel;
    100 
    101 	int			sc_state;
    102 #define	OBOE_RSLP		0x01	/* waiting for data (read) */
    103 #define	OBOE_WSLP		0x02	/* waiting for data (write) */
    104 #define OBOE_CLOSING		0x04	/* waiting for output to drain */
    105 
    106 	int			sc_speeds;
    107 	int			sc_flags;
    108 	int			sc_speed;
    109 	int			sc_ebofs;
    110 
    111 	struct oboe_dma		*sc_dmas;
    112 	struct OboeTaskFile	*sc_taskfile;    /* The taskfile   */
    113 	u_char *		sc_xmit_bufs[TX_SLOTS];
    114 	u_char *		sc_recv_bufs[RX_SLOTS];
    115 	void *			sc_xmit_stores[TX_SLOTS];
    116 	void *			sc_recv_stores[RX_SLOTS];
    117 	int			sc_txs; /* Current transmit slot number */
    118 	int			sc_rxs; /* Current receive slot number */
    119 	int			sc_saved; /* number of saved frames */
    120 	int			sc_lens[RX_SLOTS];
    121 
    122 	int			sc_txpending;
    123 
    124 	/* Statistics */
    125 	int			sc_txpackets;
    126 	int			sc_rxpackets;
    127 	int			sc_txerrors;
    128 	int			sc_rxerrors;
    129 };
    130 
    131 static int oboe_intr(void *handle);
    132 static int oboe_reset(struct oboe_softc *);
    133 
    134 struct oboe_dma {
    135 	bus_dmamap_t map;
    136 	void *addr;
    137 	bus_dma_segment_t segs[1];
    138 	int nsegs;
    139 	size_t size;
    140 	struct oboe_dma *next;
    141 };
    142 
    143 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
    144 #define KERNADDR(p) ((void *)((p)->addr))
    145 
    146 static int oboe_alloc_taskfile(struct oboe_softc *);
    147 static void oboe_init_taskfile(struct oboe_softc *);
    148 static void oboe_startchip(struct oboe_softc *);
    149 static void oboe_stopchip(struct oboe_softc *);
    150 static int oboe_setbaud(struct oboe_softc *, int);
    151 
    152 CFATTACH_DECL_NEW(oboe, sizeof(struct oboe_softc),
    153     oboe_match, oboe_attach, oboe_detach, NULL);
    154 
    155 static struct irframe_methods oboe_methods = {
    156 	oboe_open, oboe_close, oboe_read, oboe_write, oboe_poll,
    157 	oboe_kqfilter, oboe_set_params, oboe_get_speeds, oboe_get_turnarounds
    158 };
    159 
    160 static int
    161 oboe_match(device_t parent, cfdata_t match, void *aux)
    162 {
    163 	struct pci_attach_args *pa = aux;
    164 
    165 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
    166 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE ||
    167 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_DONAUOBOE))
    168 		return (1);
    169 	return 0;
    170 }
    171 
    172 static void
    173 oboe_attach(device_t parent, device_t self, void *aux)
    174 {
    175 	struct oboe_softc *sc = device_private(self);
    176 	struct pci_attach_args *pa = aux;
    177 	pci_intr_handle_t ih;
    178 	struct ir_attach_args ia;
    179 	const char *intrstring;
    180 
    181 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    182 	printf(": Toshiba Fast Infrared Type O, revision %d\n",
    183 	       sc->sc_revision);
    184 
    185 	/* Map I/O registers. */
    186 	if (pci_mapreg_map(pa, IO_BAR, PCI_MAPREG_TYPE_IO, 0,
    187 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    188 		aprint_error_dev(self, "can't map I/O space\n");
    189 		return;
    190 	}
    191 
    192 	sc->sc_dmatag = pa->pa_dmat;
    193 
    194 	ia.ia_type = IR_TYPE_IRFRAME;
    195 	ia.ia_methods = &oboe_methods;
    196 	ia.ia_handle = sc;
    197 
    198 	sc->sc_state = 0;
    199 	sc->sc_speed = IRDA_SPEED_9600;
    200 
    201 	/* Enable the device */
    202 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    203 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    204 	    PCI_COMMAND_MASTER_ENABLE);
    205 
    206 	/* Reset the device; bail out upon failure. */
    207 	if (oboe_reset(sc) != 0) {
    208 		aprint_error_dev(self, "can't reset\n");
    209 		return;
    210 	}
    211 	/* Map and establish the interrupt. */
    212 	if (pci_intr_map(pa, &ih)) {
    213 		aprint_error_dev(self, "couldn't map interrupt\n");
    214 		return;
    215 	}
    216 	intrstring = pci_intr_string(pa->pa_pc, ih);
    217 	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
    218 	if (sc->sc_ih == NULL) {
    219 		aprint_error_dev(self, "couldn't establish interrupt");
    220 		if (intrstring != NULL)
    221 			aprint_error(" at %s", intrstring);
    222 		aprint_error("\n");
    223 		return;
    224 	}
    225 	aprint_normal_dev(self, "interrupting at %s\n", intrstring);
    226 
    227 	selinit(&sc->sc_rsel);
    228 	selinit(&sc->sc_wsel);
    229 
    230 	sc->sc_txs = 0;
    231 	sc->sc_rxs = 0;
    232 
    233 	sc->sc_speeds =
    234 		IRDA_SPEED_2400   | IRDA_SPEED_9600    | IRDA_SPEED_19200 |
    235 		IRDA_SPEED_38400  | IRDA_SPEED_57600   | IRDA_SPEED_115200 |
    236 		IRDA_SPEED_576000 | IRDA_SPEED_1152000 | IRDA_SPEED_4000000;
    237 
    238 	oboe_alloc_taskfile(sc);
    239 
    240 	sc->sc_child = config_found((void *)sc, &ia, ir_print);
    241 }
    242 
    243 static int
    244 oboe_detach(device_t self, int flags)
    245 {
    246 	struct oboe_softc *sc = device_private(self);
    247 
    248 #ifdef OBOE_DEBUG
    249 	/* XXX needs reference counting for proper detach. */
    250 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    251 #endif
    252 	seldestroy(&sc->sc_rsel);
    253 	seldestroy(&sc->sc_wsel);
    254 	return (0);
    255 }
    256 
    257 static int
    258 oboe_open(void *h, int flag, int mode, struct lwp *l)
    259 {
    260 	struct oboe_softc *sc = h;
    261 
    262 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    263 
    264 	sc->sc_state = 0;
    265 	sc->sc_saved = 0;
    266 	oboe_init_taskfile(sc);
    267 	oboe_startchip(sc);
    268 
    269 	return (0);
    270 }
    271 
    272 static int
    273 oboe_close(void *h, int flag, int mode,
    274     struct lwp *l)
    275 {
    276 	struct oboe_softc *sc = h;
    277 	int error = 0;
    278 	int s = splir();
    279 
    280 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    281 	/* Wait for output to drain */
    282 
    283 	if (sc->sc_txpending > 0) {
    284 		sc->sc_state |= OBOE_CLOSING;
    285 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
    286 	}
    287 	splx(s);
    288 
    289 	oboe_stopchip(sc);
    290 	return (error);
    291 }
    292 
    293 static int
    294 oboe_read(void *h, struct uio *uio, int flag)
    295 {
    296 	struct oboe_softc *sc = h;
    297 	int error = 0;
    298 	int s;
    299 	int slot;
    300 
    301 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    302 		 __func__, uio->uio_resid, uio->uio_iovcnt,
    303 		 (long)uio->uio_offset));
    304 
    305 	s = splir();
    306 	while (sc->sc_saved == 0) {
    307 		if (flag & IO_NDELAY) {
    308 			splx(s);
    309 			return (EWOULDBLOCK);
    310 		}
    311 		sc->sc_state |= OBOE_RSLP;
    312 		DPRINTF(("oboe_read: sleep\n"));
    313 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
    314 		DPRINTF(("oboe_read: woke, error=%d\n", error));
    315 		if (error) {
    316 			sc->sc_state &= ~OBOE_RSLP;
    317 			break;
    318 		}
    319 	}
    320 
    321 	/* Do just one frame transfer per read */
    322 
    323 	if (!error) {
    324 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
    325 		if (uio->uio_resid < sc->sc_lens[slot]) {
    326 			DPRINTF(("oboe_read: uio buffer smaller than frame size"
    327 			    "(%d < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
    328 			error = EINVAL;
    329 		} else {
    330 			DPRINTF(("oboe_read: moving %d bytes from %p\n",
    331 				 sc->sc_lens[slot],
    332 				 sc->sc_recv_stores[slot]));
    333 			error = uiomove(sc->sc_recv_stores[slot],
    334 					sc->sc_lens[slot], uio);
    335 		}
    336 	}
    337 	sc->sc_saved--;
    338 	splx(s);
    339 
    340 	return (error);
    341 }
    342 
    343 static int
    344 oboe_write(void *h, struct uio *uio, int flag)
    345 {
    346 	struct oboe_softc *sc = h;
    347 	int error = 0;
    348 	int n;
    349 	int s = splir();
    350 
    351 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    352 	while (sc->sc_txpending == TX_SLOTS) {
    353 		if (flag & IO_NDELAY) {
    354 			splx(s);
    355 			return (EWOULDBLOCK);
    356 		}
    357 		sc->sc_state |= OBOE_WSLP;
    358 		DPRINTF(("oboe_write: sleep\n"));
    359 		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
    360 		DPRINTF(("oboe_write: woke up, error=%d\n", error));
    361 		if (error) {
    362 			sc->sc_state &= ~OBOE_WSLP;
    363 			break;
    364 		}
    365 	}
    366 	if (error)
    367 		goto err;
    368 	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
    369 		DPRINTF(("oboe_write: slot overrun\n"));
    370 	}
    371 
    372 	n = irda_sir_frame(sc->sc_xmit_bufs[sc->sc_txs], TX_BUF_SZ, uio,
    373 			   sc->sc_ebofs);
    374 	if (n < 0) {
    375 		error = -n;
    376 		goto err;
    377 	}
    378 	sc->sc_taskfile->xmit[sc->sc_txs].len = n;
    379 
    380 	OUTB(sc, 0, OBOE_RST);
    381 	OUTB(sc, 0x1e, OBOE_REG_11);
    382 
    383 	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
    384 
    385 	/* XXX Need delay here??? */
    386 	delay(1000);
    387 
    388 	sc->sc_txpending++;
    389 	OUTB(sc, 0x80, OBOE_RST);
    390 	OUTB(sc, 1, OBOE_REG_9);
    391 	sc->sc_txs++;
    392 	sc->sc_txs %= TX_SLOTS;
    393 
    394  err:
    395 	splx(s);
    396 	return (error);
    397 }
    398 
    399 static int
    400 oboe_set_params(void *h, struct irda_params *p)
    401 {
    402 	struct oboe_softc *sc = h;
    403 	int error;
    404 
    405 	if (p->speed > 0) {
    406 		error = oboe_setbaud(sc, p->speed);
    407 		if (error)
    408 			return (error);
    409 	}
    410 	sc->sc_ebofs = p->ebofs;
    411 
    412 	/* XXX ignore ebofs and maxsize for now */
    413 	return (0);
    414 }
    415 
    416 static int
    417 oboe_get_speeds(void *h, int *speeds)
    418 {
    419 	struct oboe_softc *sc = h;
    420 	*speeds = sc->sc_speeds;
    421 	return (0);
    422 }
    423 
    424 static int
    425 oboe_get_turnarounds(void *h, int *turnarounds)
    426 {
    427 #ifdef OBOE_DEBUG
    428 	struct oboe_softc *sc = h;
    429 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    430 #endif
    431 
    432 	/* XXX Linux driver sets all bits */
    433 	*turnarounds = IRDA_TURNT_10000; /* 10ms */
    434 
    435 	return (0);
    436 }
    437 
    438 static int
    439 oboe_poll(void *h, int events, struct lwp *l)
    440 {
    441 	struct oboe_softc *sc = h;
    442 	int revents = 0;
    443 	int s;
    444 
    445 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    446 
    447 	s = splir();
    448 	if (events & (POLLOUT | POLLWRNORM))
    449 		revents |= events & (POLLOUT | POLLWRNORM);
    450 	if (events & (POLLIN | POLLRDNORM)) {
    451 		if (sc->sc_saved > 0) {
    452 			DPRINTF(("%s: have data\n", __func__));
    453 			revents |= events & (POLLIN | POLLRDNORM);
    454 		} else {
    455 			DPRINTF(("%s: recording select\n", __func__));
    456 			selrecord(l, &sc->sc_rsel);
    457 		}
    458 	}
    459 	splx(s);
    460 
    461 	return (revents);
    462 }
    463 
    464 static void
    465 filt_oboerdetach(struct knote *kn)
    466 {
    467 	struct oboe_softc *sc = kn->kn_hook;
    468 	int s;
    469 
    470 	s = splir();
    471 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    472 	splx(s);
    473 }
    474 
    475 static int
    476 filt_oboeread(struct knote *kn, long hint)
    477 {
    478 	struct oboe_softc *sc = kn->kn_hook;
    479 
    480 	kn->kn_data = sc->sc_saved;
    481 	return (kn->kn_data > 0);
    482 }
    483 
    484 static void
    485 filt_oboewdetach(struct knote *kn)
    486 {
    487 	struct oboe_softc *sc = kn->kn_hook;
    488 	int s;
    489 
    490 	s = splir();
    491 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
    492 	splx(s);
    493 }
    494 
    495 static const struct filterops oboeread_filtops =
    496 	{ 1, NULL, filt_oboerdetach, filt_oboeread };
    497 static const struct filterops oboewrite_filtops =
    498 	{ 1, NULL, filt_oboewdetach, filt_seltrue };
    499 
    500 static int
    501 oboe_kqfilter(void *h, struct knote *kn)
    502 {
    503 	struct oboe_softc *sc = h;
    504 	struct klist *klist;
    505 	int s;
    506 
    507 	switch (kn->kn_filter) {
    508 	case EVFILT_READ:
    509 		klist = &sc->sc_rsel.sel_klist;
    510 		kn->kn_fop = &oboeread_filtops;
    511 		break;
    512 	case EVFILT_WRITE:
    513 		klist = &sc->sc_wsel.sel_klist;
    514 		kn->kn_fop = &oboewrite_filtops;
    515 		break;
    516 	default:
    517 		return (EINVAL);
    518 	}
    519 
    520 	kn->kn_hook = sc;
    521 
    522 	s = splir();
    523 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    524 	splx(s);
    525 
    526 	return (0);
    527 }
    528 
    529 static int
    530 oboe_reset(struct oboe_softc *sc)
    531 {
    532 #if 0
    533 	OUTB(sc, 0x00, OBOE_RST);
    534 	OUTB(sc, 0x80, OBOE_RST);
    535 #endif
    536 	return 0;
    537 }
    538 
    539 static int
    540 oboe_intr(void *p)
    541 {
    542 	struct oboe_softc *sc = p;
    543 	uint8_t irqstat	= INB(sc, OBOE_ISR);
    544 
    545 	if (!(irqstat & 0xf8))
    546 		return (0); /* Not for me? */
    547 
    548 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
    549 
    550 	OUTB(sc, irqstat, OBOE_ISR);
    551 
    552 	if (irqstat & OBOE_ISR_RXDONE) {
    553 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
    554 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
    555 			if (sc->sc_saved == RX_SLOTS) {
    556 				DPRINTF(("oboe_intr: all buffers filled\n"));
    557 				return 0;
    558 			}
    559 
    560 			if (len > 2)
    561 				len -= 2; /* JSP: skip check sum? */
    562 
    563 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
    564 				 sc->sc_recv_stores[sc->sc_rxs]));
    565 			memcpy(sc->sc_recv_stores[sc->sc_rxs],
    566 			       sc->sc_recv_bufs[sc->sc_rxs],
    567 			       len);
    568 			sc->sc_lens[sc->sc_rxs] = len;
    569 			sc->sc_saved++;
    570 #if 0
    571 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
    572 				     len, &sc->sc_q);
    573 #endif
    574 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
    575 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
    576 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
    577 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
    578 		}
    579 		DPRINTF(("oboe_intr no more frames available\n"));
    580 		if (sc->sc_state & OBOE_RSLP) {
    581 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
    582 			sc->sc_state &= ~OBOE_RSLP;
    583 			DPRINTF(("oboe_intr: waking up reader\n"));
    584 			wakeup(&sc->sc_rxs);
    585 		}
    586 		selnotify(&sc->sc_rsel, 0, 0);
    587 		DPRINTF(("oboe_intr returning\n"));
    588 	}
    589 	if (irqstat & OBOE_ISR_TXDONE) {
    590 	        DPRINTF(("oboe_intr: write done\n"));
    591 		sc->sc_txpending--;
    592 		sc->sc_txpackets++;
    593 
    594 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
    595 			wakeup(&sc->sc_state);
    596 			return 1;
    597 		}
    598 
    599 		if (sc->sc_state & OBOE_WSLP) {
    600 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
    601 			sc->sc_state &= ~OBOE_WSLP;
    602 			DPRINTF(("oboe_intr: waking up writer\n"));
    603 			wakeup(&sc->sc_txs);
    604 		}
    605 		selnotify(&sc->sc_wsel, 0, 0);
    606 	}
    607 	return (1);
    608 }
    609 
    610 /* XXX vtophys must go! */
    611 static void
    612 oboe_init_taskfile(struct oboe_softc *sc)
    613 {
    614 	int i;
    615 	int s = splir();
    616 
    617 	for (i = 0; i < TX_SLOTS; ++i) {
    618 		sc->sc_taskfile->xmit[i].len = 0;
    619 		sc->sc_taskfile->xmit[i].control = 0x00;
    620 		sc->sc_taskfile->xmit[i].buffer =
    621 			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
    622 	}
    623 
    624 	for (i = 0; i < RX_SLOTS; ++i) {
    625 		sc->sc_taskfile->recv[i].len = 0;
    626 		sc->sc_taskfile->recv[i].control = 0x83;
    627 		sc->sc_taskfile->recv[i].buffer =
    628 			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
    629 	}
    630 
    631 	sc->sc_txpending = 0;
    632 
    633 	splx(s);
    634 }
    635 
    636 static int
    637 oboe_alloc_taskfile(struct oboe_softc *sc)
    638 {
    639 	int i;
    640 	/* XXX */
    641 	uint32_t addr = (uint32_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
    642 	if (addr == 0) {
    643 		goto bad;
    644 	}
    645 	addr &= ~(sizeof (struct OboeTaskFile) - 1);
    646 	addr += sizeof (struct OboeTaskFile);
    647 	sc->sc_taskfile = (struct OboeTaskFile *) addr;
    648 
    649 	for (i = 0; i < TX_SLOTS; ++i) {
    650 		sc->sc_xmit_bufs[i] =
    651 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    652 		sc->sc_xmit_stores[i] =
    653 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    654 		if (sc->sc_xmit_bufs[i] == NULL ||
    655 		    sc->sc_xmit_stores[i] == NULL) {
    656 			goto bad;
    657 		}
    658 	}
    659 	for (i = 0; i < RX_SLOTS; ++i) {
    660 		sc->sc_recv_bufs[i] =
    661 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    662 		sc->sc_recv_stores[i] =
    663 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    664 		if (sc->sc_recv_bufs[i] == NULL ||
    665 		    sc->sc_recv_stores[i] == NULL) {
    666 			goto bad;
    667 		}
    668 	}
    669 
    670 	return 0;
    671 bad:
    672 	printf("oboe: malloc for buffers failed()\n");
    673 	return 1;
    674 }
    675 
    676 static void
    677 oboe_startchip (struct oboe_softc *sc)
    678 {
    679 	uint32_t physaddr;
    680 
    681 	OUTB(sc, 0, OBOE_LOCK);
    682 	OUTB(sc, 0, OBOE_RST);
    683 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
    684 	OUTB(sc, 0xf0, OBOE_REG_D);
    685 	OUTB(sc, 0xff, OBOE_ISR);
    686 	OUTB(sc, 0x0f, OBOE_REG_1A);
    687 	OUTB(sc, 0xff, OBOE_REG_1B);
    688 
    689 	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
    690 
    691 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
    692 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
    693 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
    694 
    695 	OUTB(sc, 0x0e, OBOE_REG_11);
    696 	OUTB(sc, 0x80, OBOE_RST);
    697 
    698 	(void)oboe_setbaud(sc, 9600);
    699 
    700 	sc->sc_rxs = INB(sc, OBOE_RCVT);
    701 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
    702 		sc->sc_rxs = 0;
    703 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
    704 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
    705 		sc->sc_txs = 0;
    706 }
    707 
    708 static void
    709 oboe_stopchip (struct oboe_softc *sc)
    710 {
    711 	OUTB(sc, 0x0e, OBOE_REG_11);
    712 	OUTB(sc, 0x00, OBOE_RST);
    713 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
    714 	OUTB(sc, 0xff, OBOE_TFP1);
    715 	OUTB(sc, 0xff, OBOE_TFP0);
    716 	OUTB(sc, 0x0f, OBOE_REG_1B);
    717 	OUTB(sc, 0xff, OBOE_REG_1A);
    718 	OUTB(sc, 0x00, OBOE_ISR); /* XXX: should i do this to disable ints? */
    719 	OUTB(sc, 0x80, OBOE_RST);
    720 	OUTB(sc, 0x0e, OBOE_LOCK);
    721 }
    722 
    723 #define SPEEDCASE(speed, type, divisor) \
    724 case speed: \
    725 OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
    726 OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
    727 OUTB(sc, divisor, OBOE_UDIV); \
    728 break
    729 
    730 static int
    731 oboe_setbaud(struct oboe_softc *sc, int baud)
    732 {
    733 	int s;
    734 
    735 	DPRINTF(("oboe: setting baud to %d\n", baud));
    736 
    737 	s = splir();
    738 
    739 	switch (baud) {
    740 	SPEEDCASE(   2400, SIR, 0xbf);
    741 	SPEEDCASE(   9600, SIR, 0x2f);
    742 	SPEEDCASE(  19200, SIR, 0x17);
    743 	SPEEDCASE(  38400, SIR, 0x0b);
    744 	SPEEDCASE(  57600, SIR, 0x07);
    745 	SPEEDCASE( 115200, SIR, 0x03);
    746 	SPEEDCASE(1152000, MIR, 0x01);
    747 	SPEEDCASE(4000000, FIR, 0x00);
    748 	default:
    749 		DPRINTF(("oboe: cannot set speed to %d\n", baud));
    750 		splx(s);
    751 		return (EINVAL);
    752 	}
    753 
    754 	OUTB(sc, 0x00, OBOE_RST);
    755 	OUTB(sc, 0x80, OBOE_RST);
    756 	OUTB(sc, 0x01, OBOE_REG_9);
    757 
    758 	sc->sc_speed = baud;
    759 
    760 	splx(s);
    761 
    762 	return (0);
    763 }
    764