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