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