Home | History | Annotate | Line # | Download | only in pci
oboe.c revision 1.15
      1 /*	$NetBSD: oboe.c,v 1.15 2003/10/21 03:24:25 fvdl 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.15 2003/10/21 03:24:25 fvdl 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 int oboe_match(struct device *parent, struct cfdata *match, void *aux);
     74 void oboe_attach(struct device *parent, struct device *self, void *aux);
     75 int oboe_activate(struct device *self, enum devact act);
     76 int oboe_detach(struct device *self, int flags);
     77 
     78 int oboe_open(void *h, int flag, int mode, struct proc *p);
     79 int oboe_close(void *h, int flag, int mode, struct proc *p);
     80 int oboe_read(void *h, struct uio *uio, int flag);
     81 int oboe_write(void *h, struct uio *uio, int flag);
     82 int oboe_set_params(void *h, struct irda_params *params);
     83 int oboe_get_speeds(void *h, int *speeds);
     84 int oboe_get_turnarounds(void *h, int *times);
     85 int oboe_poll(void *h, int events, struct proc *p);
     86 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 	caddr_t 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 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 int
    170 oboe_match(struct device *parent, struct cfdata *match, void *aux)
    171 {
    172 	struct pci_attach_args *pa = aux;
    173 
    174 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
    175 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE ||
    176 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_DONAUOBOE))
    177 		return (1);
    178 	return 0;
    179 }
    180 
    181 void
    182 oboe_attach(struct device *parent, struct device *self, void *aux)
    183 {
    184 	struct oboe_softc *sc = (struct oboe_softc *)self;
    185 	struct pci_attach_args *pa = aux;
    186 	pci_intr_handle_t ih;
    187 	struct ir_attach_args ia;
    188 	const char *intrstring;
    189 
    190 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    191 	printf(": Toshiba Fast Infrared Type O, revision %d\n",
    192 	       sc->sc_revision);
    193 
    194 	/* Map I/O registers. */
    195 	if (pci_mapreg_map(pa, IO_BAR, PCI_MAPREG_TYPE_IO, 0,
    196 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    197 		printf("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
    198 		return;
    199 	}
    200 
    201 	sc->sc_dmatag = pa->pa_dmat;
    202 
    203 	ia.ia_type = IR_TYPE_IRFRAME;
    204 	ia.ia_methods = &oboe_methods;
    205 	ia.ia_handle = sc;
    206 
    207 	sc->sc_state = 0;
    208 	sc->sc_speed = IRDA_SPEED_9600;
    209 
    210 	/* Enable the device */
    211 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    212 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    213 	    PCI_COMMAND_MASTER_ENABLE);
    214 
    215 	/* Reset the device; bail out upon failure. */
    216 	if (oboe_reset(sc) != 0) {
    217 		printf("%s: can't reset\n", sc->sc_dev.dv_xname);
    218 		return;
    219 	}
    220 	/* Map and establish the interrupt. */
    221 	if (pci_intr_map(pa, &ih)) {
    222 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    223 		return;
    224 	}
    225 	intrstring = pci_intr_string(pa->pa_pc, ih);
    226 	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
    227 	if (sc->sc_ih == NULL) {
    228 		printf("%s: couldn't establish interrupt",
    229 		    sc->sc_dev.dv_xname);
    230 		if (intrstring != NULL)
    231 			printf(" at %s", intrstring);
    232 		printf("\n");
    233 		return;
    234 	}
    235 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstring);
    236 
    237 	sc->sc_txs = 0;
    238 	sc->sc_rxs = 0;
    239 
    240 	sc->sc_speeds =
    241 		IRDA_SPEED_2400   | IRDA_SPEED_9600    | IRDA_SPEED_19200 |
    242 		IRDA_SPEED_38400  | IRDA_SPEED_57600   | IRDA_SPEED_115200 |
    243 		IRDA_SPEED_576000 | IRDA_SPEED_1152000 | IRDA_SPEED_4000000;
    244 
    245 	oboe_alloc_taskfile(sc);
    246 
    247 	sc->sc_child = config_found((void *)sc, &ia, ir_print);
    248 }
    249 
    250 int
    251 oboe_activate(struct device *self, enum devact act)
    252 {
    253 	struct oboe_softc *sc = (struct oboe_softc *)self;
    254 	int error;
    255 
    256 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    257 
    258 	switch (act) {
    259 	case DVACT_ACTIVATE:
    260 		return (EOPNOTSUPP);
    261 		break;
    262 
    263 	case DVACT_DEACTIVATE:
    264 		if (sc->sc_child != NULL)
    265 			error = config_deactivate(sc->sc_child);
    266 		else
    267 			error = 0;
    268 		break;
    269 	}
    270 	return (error);
    271 }
    272 
    273 int
    274 oboe_detach(struct device *self, int flags)
    275 {
    276 #if 0
    277 	struct oboe_softc *sc = (struct oboe_softc *)self;
    278 #endif
    279 	/* XXX needs reference counting for proper detach. */
    280 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    281 	return (0);
    282 }
    283 
    284 int
    285 oboe_open(void *h, int flag, int mode, struct proc *p)
    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 int
    300 oboe_close(void *h, int flag, int mode, struct proc *p)
    301 {
    302 	struct oboe_softc *sc = h;
    303 	int error = 0;
    304 	int s = splir();
    305 
    306 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    307 	/* Wait for output to drain */
    308 
    309 	if (sc->sc_txpending > 0) {
    310 		sc->sc_state |= OBOE_CLOSING;
    311 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
    312 	}
    313 	splx(s);
    314 
    315 	oboe_stopchip(sc);
    316 	return (error);
    317 }
    318 
    319 int
    320 oboe_read(void *h, struct uio *uio, int flag)
    321 {
    322 	struct oboe_softc *sc = h;
    323 	int error = 0;
    324 	int s;
    325 	int slot;
    326 
    327 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    328 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    329 		 (long)uio->uio_offset));
    330 
    331 	s = splir();
    332 	while (sc->sc_saved == 0) {
    333 		if (flag & IO_NDELAY) {
    334 			splx(s);
    335 			return (EWOULDBLOCK);
    336 		}
    337 		sc->sc_state |= OBOE_RSLP;
    338 		DPRINTF(("oboe_read: sleep\n"));
    339 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
    340 		DPRINTF(("oboe_read: woke, error=%d\n", error));
    341 		if (error) {
    342 			sc->sc_state &= ~OBOE_RSLP;
    343 			break;
    344 		}
    345 	}
    346 
    347 	/* Do just one frame transfer per read */
    348 
    349 	if (!error) {
    350 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
    351 		if (uio->uio_resid < sc->sc_lens[slot]) {
    352 			DPRINTF(("oboe_read: uio buffer smaller than frame size"
    353 			    "(%d < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
    354 			error = EINVAL;
    355 		} else {
    356 			DPRINTF(("oboe_read: moving %d bytes from %p\n",
    357 				 sc->sc_lens[slot],
    358 				 sc->sc_recv_stores[slot]));
    359 			error = uiomove(sc->sc_recv_stores[slot],
    360 					sc->sc_lens[slot], uio);
    361 		}
    362 	}
    363 	sc->sc_saved--;
    364 	splx(s);
    365 
    366 	return (error);
    367 }
    368 
    369 int
    370 oboe_write(void *h, struct uio *uio, int flag)
    371 {
    372 	struct oboe_softc *sc = h;
    373 	int error = 0;
    374 	int n;
    375 	int s = splir();
    376 
    377 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    378 	while (sc->sc_txpending == TX_SLOTS) {
    379 		if (flag & IO_NDELAY) {
    380 			splx(s);
    381 			return (EWOULDBLOCK);
    382 		}
    383 		sc->sc_state |= OBOE_WSLP;
    384 		DPRINTF(("oboe_write: sleep\n"));
    385 		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
    386 		DPRINTF(("oboe_write: woke up, error=%d\n", error));
    387 		if (error) {
    388 			sc->sc_state &= ~OBOE_WSLP;
    389 			break;
    390 		}
    391 	}
    392 	if (error)
    393 		goto err;
    394 	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
    395 		DPRINTF(("oboe_write: slot overrun\n"));
    396 	}
    397 
    398 	n = irda_sir_frame(sc->sc_xmit_bufs[sc->sc_txs], TX_BUF_SZ, uio,
    399 			   sc->sc_ebofs);
    400 	if (n < 0) {
    401 		error = -n;
    402 		goto err;
    403 	}
    404 	sc->sc_taskfile->xmit[sc->sc_txs].len = n;
    405 
    406 	OUTB(sc, 0, OBOE_RST);
    407 	OUTB(sc, 0x1e, OBOE_REG_11);
    408 
    409 	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
    410 
    411 	/* XXX Need delay here??? */
    412 	delay(1000);
    413 
    414 	sc->sc_txpending++;
    415 	OUTB(sc, 0x80, OBOE_RST);
    416 	OUTB(sc, 1, OBOE_REG_9);
    417 	sc->sc_txs++;
    418 	sc->sc_txs %= TX_SLOTS;
    419 
    420  err:
    421 	splx(s);
    422 	return (error);
    423 }
    424 
    425 int
    426 oboe_set_params(void *h, struct irda_params *p)
    427 {
    428 	struct oboe_softc *sc = h;
    429 	int error;
    430 
    431 	if (p->speed > 0) {
    432 		error = oboe_setbaud(sc, p->speed);
    433 		if (error)
    434 			return (error);
    435 	}
    436 	sc->sc_ebofs = p->ebofs;
    437 
    438 	/* XXX ignore ebofs and maxsize for now */
    439 	return (0);
    440 }
    441 
    442 int
    443 oboe_get_speeds(void *h, int *speeds)
    444 {
    445 	struct oboe_softc *sc = h;
    446 	*speeds = sc->sc_speeds;
    447 	return (0);
    448 }
    449 
    450 int
    451 oboe_get_turnarounds(void *h, int *turnarounds)
    452 {
    453 #if 0
    454 	struct oboe_softc *sc = h;
    455 #endif
    456 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    457 	/* XXX Linux driver sets all bits */
    458 	*turnarounds = IRDA_TURNT_10000; /* 10ms */
    459 	return (0);
    460 }
    461 
    462 int
    463 oboe_poll(void *h, int events, struct proc *p)
    464 {
    465 	struct oboe_softc *sc = h;
    466 	int revents = 0;
    467 	int s;
    468 
    469 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    470 
    471 	s = splir();
    472 	if (events & (POLLOUT | POLLWRNORM))
    473 		revents |= events & (POLLOUT | POLLWRNORM);
    474 	if (events & (POLLIN | POLLRDNORM)) {
    475 		if (sc->sc_saved > 0) {
    476 			DPRINTF(("%s: have data\n", __FUNCTION__));
    477 			revents |= events & (POLLIN | POLLRDNORM);
    478 		} else {
    479 			DPRINTF(("%s: recording select\n", __FUNCTION__));
    480 			selrecord(p, &sc->sc_rsel);
    481 		}
    482 	}
    483 	splx(s);
    484 
    485 	return (revents);
    486 }
    487 
    488 static void
    489 filt_oboerdetach(struct knote *kn)
    490 {
    491 	struct oboe_softc *sc = kn->kn_hook;
    492 	int s;
    493 
    494 	s = splir();
    495 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    496 	splx(s);
    497 }
    498 
    499 static int
    500 filt_oboeread(struct knote *kn, long hint)
    501 {
    502 	struct oboe_softc *sc = kn->kn_hook;
    503 
    504 	kn->kn_data = sc->sc_saved;
    505 	return (kn->kn_data > 0);
    506 }
    507 
    508 static void
    509 filt_oboewdetach(struct knote *kn)
    510 {
    511 	struct oboe_softc *sc = kn->kn_hook;
    512 	int s;
    513 
    514 	s = splir();
    515 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
    516 	splx(s);
    517 }
    518 
    519 static const struct filterops oboeread_filtops =
    520 	{ 1, NULL, filt_oboerdetach, filt_oboeread };
    521 static const struct filterops oboewrite_filtops =
    522 	{ 1, NULL, filt_oboewdetach, filt_seltrue };
    523 
    524 int
    525 oboe_kqfilter(void *h, struct knote *kn)
    526 {
    527 	struct oboe_softc *sc = h;
    528 	struct klist *klist;
    529 	int s;
    530 
    531 	switch (kn->kn_filter) {
    532 	case EVFILT_READ:
    533 		klist = &sc->sc_rsel.sel_klist;
    534 		kn->kn_fop = &oboeread_filtops;
    535 		break;
    536 	case EVFILT_WRITE:
    537 		klist = &sc->sc_wsel.sel_klist;
    538 		kn->kn_fop = &oboewrite_filtops;
    539 		break;
    540 	default:
    541 		return (1);
    542 	}
    543 
    544 	kn->kn_hook = sc;
    545 
    546 	s = splir();
    547 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    548 	splx(s);
    549 
    550 	return (0);
    551 }
    552 
    553 static int
    554 oboe_reset(struct oboe_softc *sc)
    555 {
    556 #if 0
    557 	OUTB(sc, 0x00, OBOE_RST);
    558 	OUTB(sc, 0x80, OBOE_RST);
    559 #endif
    560 	return 0;
    561 }
    562 
    563 static int
    564 oboe_intr(void *p)
    565 {
    566 	struct oboe_softc *sc = p;
    567 	uint8_t irqstat	= INB(sc, OBOE_ISR);
    568 
    569 	if (!(irqstat & 0xf8))
    570 		return (0); /* Not for me? */
    571 
    572 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
    573 
    574 	OUTB(sc, irqstat, OBOE_ISR);
    575 
    576 	if (irqstat & OBOE_ISR_RXDONE) {
    577 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
    578 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
    579 			if (sc->sc_saved == RX_SLOTS) {
    580 				DPRINTF(("oboe_intr: all buffers filled\n"));
    581 				return 0;
    582 			}
    583 
    584 			if (len > 2)
    585 				len -= 2; /* JSP: skip check sum? */
    586 
    587 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
    588 				 sc->sc_recv_stores[sc->sc_rxs]));
    589 			memcpy(sc->sc_recv_stores[sc->sc_rxs],
    590 			       sc->sc_recv_bufs[sc->sc_rxs],
    591 			       len);
    592 			sc->sc_lens[sc->sc_rxs] = len;
    593 			sc->sc_saved++;
    594 #if 0
    595 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
    596 				     len, &sc->sc_q);
    597 #endif
    598 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
    599 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
    600 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
    601 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
    602 		}
    603 		DPRINTF(("oboe_intr no more frames available\n"));
    604 		if (sc->sc_state & OBOE_RSLP) {
    605 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
    606 			sc->sc_state &= ~OBOE_RSLP;
    607 			DPRINTF(("oboe_intr: waking up reader\n"));
    608 			wakeup(&sc->sc_rxs);
    609 		}
    610 		selnotify(&sc->sc_rsel, 0);
    611 		DPRINTF(("oboe_intr returning\n"));
    612 	}
    613 	if (irqstat & OBOE_ISR_TXDONE) {
    614 	        DPRINTF(("oboe_intr: write done\n"));
    615 		sc->sc_txpending--;
    616 		sc->sc_txpackets++;
    617 
    618 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
    619 			wakeup(&sc->sc_state);
    620 			return 1;
    621 		}
    622 
    623 		if (sc->sc_state & OBOE_WSLP) {
    624 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
    625 			sc->sc_state &= ~OBOE_WSLP;
    626 			DPRINTF(("oboe_intr: waking up writer\n"));
    627 			wakeup(&sc->sc_txs);
    628 		}
    629 		selnotify(&sc->sc_wsel, 0);
    630 	}
    631 	return (1);
    632 }
    633 
    634 /* XXX vtophys must go! */
    635 static void
    636 oboe_init_taskfile(struct oboe_softc *sc)
    637 {
    638 	int i;
    639 	int s = splir();
    640 
    641 	for (i = 0; i < TX_SLOTS; ++i) {
    642 		sc->sc_taskfile->xmit[i].len = 0;
    643 		sc->sc_taskfile->xmit[i].control = 0x00;
    644 		sc->sc_taskfile->xmit[i].buffer =
    645 			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
    646 	}
    647 
    648 	for (i = 0; i < RX_SLOTS; ++i) {
    649 		sc->sc_taskfile->recv[i].len = 0;
    650 		sc->sc_taskfile->recv[i].control = 0x83;
    651 		sc->sc_taskfile->recv[i].buffer =
    652 			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
    653 	}
    654 
    655 	sc->sc_txpending = 0;
    656 
    657 	splx(s);
    658 }
    659 
    660 static int
    661 oboe_alloc_taskfile(struct oboe_softc *sc)
    662 {
    663 	int i;
    664 	/* XXX */
    665 	uint32_t addr = (uint32_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
    666 	if (addr == 0) {
    667 		goto bad;
    668 	}
    669 	addr &= ~(sizeof (struct OboeTaskFile) - 1);
    670 	addr += sizeof (struct OboeTaskFile);
    671 	sc->sc_taskfile = (struct OboeTaskFile *) addr;
    672 
    673 	for (i = 0; i < TX_SLOTS; ++i) {
    674 		sc->sc_xmit_bufs[i] =
    675 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    676 		sc->sc_xmit_stores[i] =
    677 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    678 		if (sc->sc_xmit_bufs[i] == NULL ||
    679 		    sc->sc_xmit_stores[i] == NULL) {
    680 			goto bad;
    681 		}
    682 	}
    683 	for (i = 0; i < RX_SLOTS; ++i) {
    684 		sc->sc_recv_bufs[i] =
    685 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    686 		sc->sc_recv_stores[i] =
    687 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    688 		if (sc->sc_recv_bufs[i] == NULL ||
    689 		    sc->sc_recv_stores[i] == NULL) {
    690 			goto bad;
    691 		}
    692 	}
    693 
    694 	return 0;
    695 bad:
    696 	printf("oboe: malloc for buffers failed()\n");
    697 	return 1;
    698 }
    699 
    700 static void
    701 oboe_startchip (struct oboe_softc *sc)
    702 {
    703 	uint32_t physaddr;
    704 
    705 	OUTB(sc, 0, OBOE_LOCK);
    706 	OUTB(sc, 0, OBOE_RST);
    707 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
    708 	OUTB(sc, 0xf0, OBOE_REG_D);
    709 	OUTB(sc, 0xff, OBOE_ISR);
    710 	OUTB(sc, 0x0f, OBOE_REG_1A);
    711 	OUTB(sc, 0xff, OBOE_REG_1B);
    712 
    713 	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
    714 
    715 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
    716 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
    717 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
    718 
    719 	OUTB(sc, 0x0e, OBOE_REG_11);
    720 	OUTB(sc, 0x80, OBOE_RST);
    721 
    722 	(void)oboe_setbaud(sc, 9600);
    723 
    724 	sc->sc_rxs = INB(sc, OBOE_RCVT);
    725 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
    726 		sc->sc_rxs = 0;
    727 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
    728 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
    729 		sc->sc_txs = 0;
    730 }
    731 
    732 static void
    733 oboe_stopchip (struct oboe_softc *sc)
    734 {
    735 	OUTB(sc, 0x0e, OBOE_REG_11);
    736 	OUTB(sc, 0x00, OBOE_RST);
    737 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
    738 	OUTB(sc, 0xff, OBOE_TFP1);
    739 	OUTB(sc, 0xff, OBOE_TFP0);
    740 	OUTB(sc, 0x0f, OBOE_REG_1B);
    741 	OUTB(sc, 0xff, OBOE_REG_1A);
    742 	OUTB(sc, 0x00, OBOE_ISR); /* XXX: should i do this to disable ints? */
    743 	OUTB(sc, 0x80, OBOE_RST);
    744 	OUTB(sc, 0x0e, OBOE_LOCK);
    745 }
    746 
    747 #define SPEEDCASE(speed, type, divisor) \
    748 case speed: \
    749 OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
    750 OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
    751 OUTB(sc, divisor, OBOE_UDIV); \
    752 break
    753 
    754 static int
    755 oboe_setbaud(struct oboe_softc *sc, int baud)
    756 {
    757 	int s;
    758 
    759 	DPRINTF(("oboe: setting baud to %d\n", baud));
    760 
    761 	s = splir();
    762 
    763 	switch (baud) {
    764 	SPEEDCASE(   2400, SIR, 0xbf);
    765 	SPEEDCASE(   9600, SIR, 0x2f);
    766 	SPEEDCASE(  19200, SIR, 0x17);
    767 	SPEEDCASE(  38400, SIR, 0x0b);
    768 	SPEEDCASE(  57600, SIR, 0x07);
    769 	SPEEDCASE( 115200, SIR, 0x03);
    770 	SPEEDCASE(1152000, MIR, 0x01);
    771 	SPEEDCASE(4000000, FIR, 0x00);
    772 	default:
    773 		DPRINTF(("oboe: cannot set speed to %d\n", baud));
    774 		splx(s);
    775 		return (EINVAL);
    776 	}
    777 
    778 	OUTB(sc, 0x00, OBOE_RST);
    779 	OUTB(sc, 0x80, OBOE_RST);
    780 	OUTB(sc, 0x01, OBOE_REG_9);
    781 
    782 	sc->sc_speed = baud;
    783 
    784 	splx(s);
    785 
    786 	return (0);
    787 }
    788