Home | History | Annotate | Line # | Download | only in pci
oboe.c revision 1.8
      1 /*	$NetBSD: oboe.c,v 1.8 2002/10/02 16:51:47 thorpej 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 
     82 #ifdef OBOE_DEBUG
     83 #define DPRINTF(x)	if (oboedebug) printf x
     84 int oboedebug = 1;
     85 #else
     86 #define DPRINTF(x)
     87 #endif
     88 
     89 struct oboe_dma;
     90 
     91 struct oboe_softc {
     92 	struct device		sc_dev;
     93 	struct device		*sc_child;
     94 	struct pci_attach_args	sc_pa;
     95 	pci_intr_handle_t *	sc_ih;
     96 	unsigned int		sc_revision;	/* PCI Revision ID */
     97 	/* I/O Base device */
     98 	bus_space_tag_t		sc_iot;
     99 	bus_space_handle_t	sc_ioh;
    100 	bus_dma_tag_t		sc_dmatag;
    101 	struct selinfo		sc_rsel;
    102 
    103 	int			sc_state;
    104 #define	OBOE_RSLP		0x01	/* waiting for data (read) */
    105 #define	OBOE_WSLP		0x02	/* waiting for data (write) */
    106 #define OBOE_CLOSING		0x04	/* waiting for output to drain */
    107 
    108 	int			sc_speeds;
    109 	int			sc_flags;
    110 	int			sc_speed;
    111 	int			sc_ebofs;
    112 
    113 	struct oboe_dma		*sc_dmas;
    114 	struct OboeTaskFile	*sc_taskfile;    /* The taskfile   */
    115 	u_char *		sc_xmit_bufs[TX_SLOTS];
    116 	u_char *		sc_recv_bufs[RX_SLOTS];
    117 	void *			sc_xmit_stores[TX_SLOTS];
    118 	void *			sc_recv_stores[RX_SLOTS];
    119 	int			sc_txs; /* Current transmit slot number */
    120 	int			sc_rxs; /* Current receive slot number */
    121 	int			sc_saved; /* number of saved frames */
    122 	int			sc_lens[RX_SLOTS];
    123 
    124 	int			sc_txpending;
    125 
    126 	/* Statistics */
    127 	int			sc_txpackets;
    128 	int			sc_rxpackets;
    129 	int			sc_txerrors;
    130 	int			sc_rxerrors;
    131 };
    132 
    133 static int oboe_intr(void *handle);
    134 static int oboe_reset(struct oboe_softc *);
    135 
    136 struct oboe_dma {
    137 	bus_dmamap_t map;
    138 	caddr_t addr;
    139 	bus_dma_segment_t segs[1];
    140 	int nsegs;
    141 	size_t size;
    142 	struct oboe_dma *next;
    143 };
    144 
    145 #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
    146 #define KERNADDR(p) ((void *)((p)->addr))
    147 
    148 static int oboe_alloc_taskfile(struct oboe_softc *);
    149 static void oboe_init_taskfile(struct oboe_softc *);
    150 static void oboe_startchip(struct oboe_softc *);
    151 static void oboe_stopchip(struct oboe_softc *);
    152 static int oboe_setbaud(struct oboe_softc *, int);
    153 
    154 CFATTACH_DECL(oboe, sizeof(struct oboe_softc),
    155     oboe_match, oboe_attach, oboe_detach, oboe_activate);
    156 
    157 struct irframe_methods oboe_methods = {
    158 	oboe_open, oboe_close, oboe_read, oboe_write, oboe_poll,
    159 	oboe_set_params, oboe_get_speeds, oboe_get_turnarounds
    160 };
    161 
    162 int
    163 oboe_match(struct device *parent, struct cfdata *match, void *aux)
    164 {
    165 	struct pci_attach_args *pa = aux;
    166 
    167 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
    168 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE ||
    169 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_DONAUOBOE))
    170 		return (1);
    171 	return 0;
    172 }
    173 
    174 void
    175 oboe_attach(struct device *parent, struct device *self, void *aux)
    176 {
    177 	struct oboe_softc *sc = (struct oboe_softc *)self;
    178 	struct pci_attach_args *pa = aux;
    179 	pci_intr_handle_t ih;
    180 	struct ir_attach_args ia;
    181 	const char *intrstring;
    182 
    183 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    184 	printf(": Toshiba Fast Infrared Type O, revision %d\n",
    185 	       sc->sc_revision);
    186 
    187 	/* Map I/O registers. */
    188 	if (pci_mapreg_map(pa, IO_BAR, PCI_MAPREG_TYPE_IO, 0,
    189 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    190 		printf("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
    191 		return;
    192 	}
    193 
    194 	sc->sc_dmatag = pa->pa_dmat;
    195 
    196 	ia.ia_type = IR_TYPE_IRFRAME;
    197 	ia.ia_methods = &oboe_methods;
    198 	ia.ia_handle = sc;
    199 
    200 	sc->sc_state = 0;
    201 	sc->sc_speed = IRDA_SPEED_9600;
    202 
    203 	/* Enable the device */
    204 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    205 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    206 	    PCI_COMMAND_MASTER_ENABLE);
    207 
    208 	/* Reset the device; bail out upon failure. */
    209 	if (oboe_reset(sc) != 0) {
    210 		printf("%s: can't reset\n", sc->sc_dev.dv_xname);
    211 		return;
    212 	}
    213 	/* Map and establish the interrupt. */
    214 	if (pci_intr_map(pa, &ih)) {
    215 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    216 		return;
    217 	}
    218 	intrstring = pci_intr_string(pa->pa_pc, ih);
    219 	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
    220 	if (sc->sc_ih == NULL) {
    221 		printf("%s: couldn't establish interrupt",
    222 		    sc->sc_dev.dv_xname);
    223 		if (intrstring != NULL)
    224 			printf(" at %s", intrstring);
    225 		printf("\n");
    226 		return;
    227 	}
    228 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstring);
    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 int
    244 oboe_activate(struct device *self, enum devact act)
    245 {
    246 	struct oboe_softc *sc = (struct oboe_softc *)self;
    247 	int error;
    248 
    249 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    250 
    251 	switch (act) {
    252 	case DVACT_ACTIVATE:
    253 		return (EOPNOTSUPP);
    254 		break;
    255 
    256 	case DVACT_DEACTIVATE:
    257 		if (sc->sc_child != NULL)
    258 			error = config_deactivate(sc->sc_child);
    259 		else
    260 			error = 0;
    261 		break;
    262 	}
    263 	return (error);
    264 }
    265 
    266 int
    267 oboe_detach(struct device *self, int flags)
    268 {
    269 #if 0
    270 	struct oboe_softc *sc = (struct oboe_softc *)self;
    271 #endif
    272 	/* XXX needs reference counting for proper detach. */
    273 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    274 	return (0);
    275 }
    276 
    277 int
    278 oboe_open(void *h, int flag, int mode, struct proc *p)
    279 {
    280 	struct oboe_softc *sc = h;
    281 
    282 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    283 
    284 	sc->sc_state = 0;
    285 	sc->sc_saved = 0;
    286 	oboe_init_taskfile(sc);
    287 	oboe_startchip(sc);
    288 
    289 	return (0);
    290 }
    291 
    292 int
    293 oboe_close(void *h, int flag, int mode, struct proc *p)
    294 {
    295 	struct oboe_softc *sc = h;
    296 	int error = 0;
    297 	int s = splir();
    298 
    299 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    300 	/* Wait for output to drain */
    301 
    302 	if (sc->sc_txpending > 0) {
    303 		sc->sc_state |= OBOE_CLOSING;
    304 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
    305 	}
    306 	splx(s);
    307 
    308 	oboe_stopchip(sc);
    309 	return (error);
    310 }
    311 
    312 int
    313 oboe_read(void *h, struct uio *uio, int flag)
    314 {
    315 	struct oboe_softc *sc = h;
    316 	int error = 0;
    317 	int s;
    318 	int slot;
    319 
    320 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    321 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    322 		 (long)uio->uio_offset));
    323 
    324 	s = splir();
    325 	while (sc->sc_saved == 0) {
    326 		if (flag & IO_NDELAY) {
    327 			splx(s);
    328 			return (EWOULDBLOCK);
    329 		}
    330 		sc->sc_state |= OBOE_RSLP;
    331 		DPRINTF(("oboe_read: sleep\n"));
    332 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
    333 		DPRINTF(("oboe_read: woke, error=%d\n", error));
    334 		if (error) {
    335 			sc->sc_state &= ~OBOE_RSLP;
    336 			break;
    337 		}
    338 	}
    339 
    340 	/* Do just one frame transfer per read */
    341 
    342 	if (!error) {
    343 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
    344 		if (uio->uio_resid < sc->sc_lens[slot]) {
    345 			DPRINTF(("oboe_read: uio buffer smaller than frame size"
    346 			    "(%d < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
    347 			error = EINVAL;
    348 		} else {
    349 			DPRINTF(("oboe_read: moving %d bytes from %p\n",
    350 				 sc->sc_lens[slot],
    351 				 sc->sc_recv_stores[slot]));
    352 			error = uiomove(sc->sc_recv_stores[slot],
    353 					sc->sc_lens[slot], uio);
    354 		}
    355 	}
    356 	sc->sc_saved--;
    357 	splx(s);
    358 
    359 	return (error);
    360 }
    361 
    362 int
    363 oboe_write(void *h, struct uio *uio, int flag)
    364 {
    365 	struct oboe_softc *sc = h;
    366 	int error = 0;
    367 	int n;
    368 	int s = splir();
    369 
    370 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    371 	while (sc->sc_txpending == TX_SLOTS) {
    372 		if (flag & IO_NDELAY) {
    373 			splx(s);
    374 			return (EWOULDBLOCK);
    375 		}
    376 		sc->sc_state |= OBOE_WSLP;
    377 		DPRINTF(("oboe_write: sleep\n"));
    378 		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
    379 		DPRINTF(("oboe_write: woke up, error=%d\n", error));
    380 		if (error) {
    381 			sc->sc_state &= ~OBOE_WSLP;
    382 			break;
    383 		}
    384 	}
    385 	if (error)
    386 		goto err;
    387 	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
    388 		DPRINTF(("oboe_write: slot overrun\n"));
    389 	}
    390 
    391 	n = irda_sir_frame(sc->sc_xmit_bufs[sc->sc_txs], TX_BUF_SZ, uio,
    392 			   sc->sc_ebofs);
    393 	if (n < 0) {
    394 		error = -n;
    395 		goto err;
    396 	}
    397 	sc->sc_taskfile->xmit[sc->sc_txs].len = n;
    398 
    399 	OUTB(sc, 0, OBOE_RST);
    400 	OUTB(sc, 0x1e, OBOE_REG_11);
    401 
    402 	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
    403 
    404 	/* XXX Need delay here??? */
    405 	delay(1000);
    406 
    407 	sc->sc_txpending++;
    408 	OUTB(sc, 0x80, OBOE_RST);
    409 	OUTB(sc, 1, OBOE_REG_9);
    410 	sc->sc_txs++;
    411 	sc->sc_txs %= TX_SLOTS;
    412 
    413  err:
    414 	splx(s);
    415 	return (error);
    416 }
    417 
    418 int
    419 oboe_set_params(void *h, struct irda_params *p)
    420 {
    421 	struct oboe_softc *sc = h;
    422 	int error;
    423 
    424 	if (p->speed > 0) {
    425 		error = oboe_setbaud(sc, p->speed);
    426 		if (error)
    427 			return (error);
    428 	}
    429 	sc->sc_ebofs = p->ebofs;
    430 
    431 	/* XXX ignore ebofs and maxsize for now */
    432 	return (0);
    433 }
    434 
    435 int
    436 oboe_get_speeds(void *h, int *speeds)
    437 {
    438 	struct oboe_softc *sc = h;
    439 	*speeds = sc->sc_speeds;
    440 	return (0);
    441 }
    442 
    443 int
    444 oboe_get_turnarounds(void *h, int *turnarounds)
    445 {
    446 #if 0
    447 	struct oboe_softc *sc = h;
    448 #endif
    449 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    450 	/* XXX Linux driver sets all bits */
    451 	*turnarounds = IRDA_TURNT_10000; /* 10ms */
    452 	return (0);
    453 }
    454 
    455 int
    456 oboe_poll(void *h, int events, struct proc *p)
    457 {
    458 	struct oboe_softc *sc = h;
    459 	int revents = 0;
    460 	int s;
    461 
    462 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    463 
    464 	s = splir();
    465 	if (events & (POLLOUT | POLLWRNORM))
    466 		revents |= events & (POLLOUT | POLLWRNORM);
    467 	if (events & (POLLIN | POLLRDNORM)) {
    468 		if (sc->sc_saved > 0) {
    469 			DPRINTF(("%s: have data\n", __FUNCTION__));
    470 			revents |= events & (POLLIN | POLLRDNORM);
    471 		} else {
    472 			DPRINTF(("%s: recording select\n", __FUNCTION__));
    473 			selrecord(p, &sc->sc_rsel);
    474 		}
    475 	}
    476 	splx(s);
    477 
    478 	return (revents);
    479 }
    480 
    481 
    482 static int
    483 oboe_reset(struct oboe_softc *sc)
    484 {
    485 #if 0
    486 	OUTB(sc, 0x00, OBOE_RST);
    487 	OUTB(sc, 0x80, OBOE_RST);
    488 #endif
    489 	return 0;
    490 }
    491 
    492 static int
    493 oboe_intr(void *p)
    494 {
    495 	struct oboe_softc *sc = p;
    496 	uint8_t irqstat	= INB(sc, OBOE_ISR);
    497 
    498 	if (!(irqstat & 0xf8))
    499 		return (0); /* Not for me? */
    500 
    501 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
    502 
    503 	OUTB(sc, irqstat, OBOE_ISR);
    504 
    505 	if (irqstat & OBOE_ISR_RXDONE) {
    506 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
    507 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
    508 			if (sc->sc_saved == RX_SLOTS) {
    509 				DPRINTF(("oboe_intr: all buffers filled\n"));
    510 				return 0;
    511 			}
    512 
    513 			if (len > 2)
    514 				len -= 2; /* JSP: skip check sum? */
    515 
    516 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
    517 				 sc->sc_recv_stores[sc->sc_rxs]));
    518 			memcpy(sc->sc_recv_stores[sc->sc_rxs],
    519 			       sc->sc_recv_bufs[sc->sc_rxs],
    520 			       len);
    521 			sc->sc_lens[sc->sc_rxs] = len;
    522 			sc->sc_saved++;
    523 #if 0
    524 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
    525 				     len, &sc->sc_q);
    526 #endif
    527 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
    528 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
    529 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
    530 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
    531 		}
    532 		DPRINTF(("oboe_intr no more frames available\n"));
    533 		if (sc->sc_state & OBOE_RSLP) {
    534 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
    535 			sc->sc_state &= ~OBOE_RSLP;
    536 			DPRINTF(("oboe_intr: waking up reader\n"));
    537 			wakeup(&sc->sc_rxs);
    538 		}
    539 		selwakeup(&sc->sc_rsel);
    540 		DPRINTF(("oboe_intr returning\n"));
    541 	}
    542 	if (irqstat & OBOE_ISR_TXDONE) {
    543 	        DPRINTF(("oboe_intr: write done\n"));
    544 		sc->sc_txpending--;
    545 		sc->sc_txpackets++;
    546 
    547 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
    548 			wakeup(&sc->sc_state);
    549 			return 1;
    550 		}
    551 
    552 		if (sc->sc_state & OBOE_WSLP) {
    553 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
    554 			sc->sc_state &= ~OBOE_WSLP;
    555 			DPRINTF(("oboe_intr: waking up writer\n"));
    556 			wakeup(&sc->sc_txs);
    557 		}
    558 	}
    559 	return (1);
    560 }
    561 
    562 /* XXX vtophys must go! */
    563 static void
    564 oboe_init_taskfile(struct oboe_softc *sc)
    565 {
    566 	int i;
    567 	int s = splir();
    568 
    569 	for (i = 0; i < TX_SLOTS; ++i) {
    570 		sc->sc_taskfile->xmit[i].len = 0;
    571 		sc->sc_taskfile->xmit[i].control = 0x00;
    572 		sc->sc_taskfile->xmit[i].buffer =
    573 			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
    574 	}
    575 
    576 	for (i = 0; i < RX_SLOTS; ++i) {
    577 		sc->sc_taskfile->recv[i].len = 0;
    578 		sc->sc_taskfile->recv[i].control = 0x83;
    579 		sc->sc_taskfile->recv[i].buffer =
    580 			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
    581 	}
    582 
    583 	sc->sc_txpending = 0;
    584 
    585 	splx(s);
    586 }
    587 
    588 static int
    589 oboe_alloc_taskfile(struct oboe_softc *sc)
    590 {
    591 	int i;
    592 	/* XXX */
    593 	uint32_t addr = (uint32_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
    594 	if (addr == NULL) {
    595 		goto bad;
    596 	}
    597 	addr &= ~(sizeof (struct OboeTaskFile) - 1);
    598 	addr += sizeof (struct OboeTaskFile);
    599 	sc->sc_taskfile = (struct OboeTaskFile *) addr;
    600 
    601 	for (i = 0; i < TX_SLOTS; ++i) {
    602 		sc->sc_xmit_bufs[i] =
    603 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    604 		sc->sc_xmit_stores[i] =
    605 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    606 		if (sc->sc_xmit_bufs[i] == NULL ||
    607 		    sc->sc_xmit_stores[i] == NULL) {
    608 			goto bad;
    609 		}
    610 	}
    611 	for (i = 0; i < RX_SLOTS; ++i) {
    612 		sc->sc_recv_bufs[i] =
    613 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    614 		sc->sc_recv_stores[i] =
    615 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    616 		if (sc->sc_recv_bufs[i] == NULL ||
    617 		    sc->sc_recv_stores[i] == NULL) {
    618 			goto bad;
    619 		}
    620 	}
    621 
    622 	return 0;
    623 bad:
    624 	printf("oboe: malloc for buffers failed()\n");
    625 	return 1;
    626 }
    627 
    628 static void
    629 oboe_startchip (struct oboe_softc *sc)
    630 {
    631 	uint32_t physaddr;
    632 
    633 	OUTB(sc, 0, OBOE_LOCK);
    634 	OUTB(sc, 0, OBOE_RST);
    635 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
    636 	OUTB(sc, 0xf0, OBOE_REG_D);
    637 	OUTB(sc, 0xff, OBOE_ISR);
    638 	OUTB(sc, 0x0f, OBOE_REG_1A);
    639 	OUTB(sc, 0xff, OBOE_REG_1B);
    640 
    641 	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
    642 
    643 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
    644 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
    645 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
    646 
    647 	OUTB(sc, 0x0e, OBOE_REG_11);
    648 	OUTB(sc, 0x80, OBOE_RST);
    649 
    650 	(void)oboe_setbaud(sc, 9600);
    651 
    652 	sc->sc_rxs = INB(sc, OBOE_RCVT);
    653 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
    654 		sc->sc_rxs = 0;
    655 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
    656 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
    657 		sc->sc_txs = 0;
    658 }
    659 
    660 static void
    661 oboe_stopchip (struct oboe_softc *sc)
    662 {
    663 	OUTB(sc, 0x0e, OBOE_REG_11);
    664 	OUTB(sc, 0x00, OBOE_RST);
    665 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
    666 	OUTB(sc, 0xff, OBOE_TFP1);
    667 	OUTB(sc, 0xff, OBOE_TFP0);
    668 	OUTB(sc, 0x0f, OBOE_REG_1B);
    669 	OUTB(sc, 0xff, OBOE_REG_1A);
    670 	OUTB(sc, 0x00, OBOE_ISR); /* XXX: should i do this to disable ints? */
    671 	OUTB(sc, 0x80, OBOE_RST);
    672 	OUTB(sc, 0x0e, OBOE_LOCK);
    673 }
    674 
    675 #define SPEEDCASE(speed, type, divisor) \
    676 case speed: \
    677 OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
    678 OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
    679 OUTB(sc, divisor, OBOE_UDIV); \
    680 break
    681 
    682 static int
    683 oboe_setbaud(struct oboe_softc *sc, int baud)
    684 {
    685 	int s;
    686 
    687 	DPRINTF(("oboe: setting baud to %d\n", baud));
    688 
    689 	s = splir();
    690 
    691 	switch (baud) {
    692 	SPEEDCASE(   2400, SIR, 0xbf);
    693 	SPEEDCASE(   9600, SIR, 0x2f);
    694 	SPEEDCASE(  19200, SIR, 0x17);
    695 	SPEEDCASE(  38400, SIR, 0x0b);
    696 	SPEEDCASE(  57600, SIR, 0x07);
    697 	SPEEDCASE( 115200, SIR, 0x03);
    698 	SPEEDCASE(1152000, MIR, 0x01);
    699 	SPEEDCASE(4000000, FIR, 0x00);
    700 	default:
    701 		DPRINTF(("oboe: cannot set speed to %d\n", baud));
    702 		splx(s);
    703 		return (EINVAL);
    704 	}
    705 
    706 	OUTB(sc, 0x00, OBOE_RST);
    707 	OUTB(sc, 0x80, OBOE_RST);
    708 	OUTB(sc, 0x01, OBOE_REG_9);
    709 
    710 	sc->sc_speed = baud;
    711 
    712 	splx(s);
    713 
    714 	return (0);
    715 }
    716