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