Home | History | Annotate | Line # | Download | only in pci
oboe.c revision 1.4
      1 /*	$NetBSD: oboe.c,v 1.4 2001/12/05 15:51:11 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 #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 void oboe_setbaud(struct oboe_softc *, int);
    153 
    154 struct cfattach oboe_ca = {
    155 	sizeof(struct oboe_softc), oboe_match, oboe_attach,
    156 	oboe_detach, oboe_activate
    157 };
    158 
    159 struct irframe_methods oboe_methods = {
    160 	oboe_open, oboe_close, oboe_read, oboe_write, oboe_poll,
    161 	oboe_set_params, oboe_get_speeds, oboe_get_turnarounds
    162 };
    163 
    164 int
    165 oboe_match(struct device *parent, struct cfdata *match, void *aux)
    166 {
    167 	struct pci_attach_args *pa = aux;
    168 
    169 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
    170 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE)
    171 		return (1);
    172 	return 0;
    173 }
    174 
    175 void
    176 oboe_attach(struct device *parent, struct device *self, void *aux)
    177 {
    178 	struct oboe_softc *sc = (struct oboe_softc *)self;
    179 	struct pci_attach_args *pa = aux;
    180 	pci_intr_handle_t ih;
    181 	struct ir_attach_args ia;
    182 	const char *intrstring;
    183 
    184 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    185 	printf(": Toshiba Fast Infrared Type O, revision %d\n",
    186 	       sc->sc_revision);
    187 
    188 	/* Map I/O registers. */
    189 	if (pci_mapreg_map(pa, IO_BAR, PCI_MAPREG_TYPE_IO, 0,
    190 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    191 		printf("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
    192 		return;
    193 	}
    194 
    195 	sc->sc_dmatag = pa->pa_dmat;
    196 
    197 	ia.ia_type = IR_TYPE_IRFRAME;
    198 	ia.ia_methods = &oboe_methods;
    199 	ia.ia_handle = sc;
    200 
    201 	sc->sc_state = 0;
    202 	sc->sc_speed = IRDA_SPEED_9600;
    203 
    204 	/* Enable the device */
    205 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    206 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    207 	    PCI_COMMAND_MASTER_ENABLE);
    208 
    209 	/* Reset the device; bail out upon failure. */
    210 	if (oboe_reset(sc) != 0) {
    211 		printf("%s: can't reset\n", sc->sc_dev.dv_xname);
    212 		return;
    213 	}
    214 	/* Map and establish the interrupt. */
    215 	if (pci_intr_map(pa, &ih)) {
    216 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    217 		return;
    218 	}
    219 	intrstring = pci_intr_string(pa->pa_pc, ih);
    220 	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
    221 	if (sc->sc_ih == NULL) {
    222 		printf("%s: couldn't establish interrupt",
    223 		    sc->sc_dev.dv_xname);
    224 		if (intrstring != NULL)
    225 			printf(" at %s", intrstring);
    226 		printf("\n");
    227 		return;
    228 	}
    229 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstring);
    230 
    231 	sc->sc_txs = 0;
    232 	sc->sc_rxs = 0;
    233 
    234 	sc->sc_speeds =
    235 		IRDA_SPEED_2400   | IRDA_SPEED_9600    | IRDA_SPEED_19200 |
    236 		IRDA_SPEED_38400  | IRDA_SPEED_57600   | IRDA_SPEED_115200 |
    237 		IRDA_SPEED_576000 | IRDA_SPEED_1152000 | IRDA_SPEED_4000000;
    238 
    239 	oboe_alloc_taskfile(sc);
    240 
    241 	sc->sc_child = config_found((void *)sc, &ia, ir_print);
    242 }
    243 
    244 int
    245 oboe_activate(struct device *self, enum devact act)
    246 {
    247 	struct oboe_softc *sc = (struct oboe_softc *)self;
    248 	int error;
    249 
    250 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    251 
    252 	switch (act) {
    253 	case DVACT_ACTIVATE:
    254 		return (EOPNOTSUPP);
    255 		break;
    256 
    257 	case DVACT_DEACTIVATE:
    258 		if (sc->sc_child != NULL)
    259 			error = config_deactivate(sc->sc_child);
    260 		else
    261 			error = 0;
    262 		break;
    263 	}
    264 	return (error);
    265 }
    266 
    267 int
    268 oboe_detach(struct device *self, int flags)
    269 {
    270 #if 0
    271 	struct oboe_softc *sc = (struct oboe_softc *)self;
    272 #endif
    273 	/* XXX needs reference counting for proper detach. */
    274 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    275 	return (0);
    276 }
    277 
    278 int
    279 oboe_open(void *h, int flag, int mode, struct proc *p)
    280 {
    281 	struct oboe_softc *sc = h;
    282 
    283 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    284 
    285 	sc->sc_state = 0;
    286 	sc->sc_saved = 0;
    287 	oboe_init_taskfile(sc);
    288 	oboe_startchip(sc);
    289 
    290 	return (0);
    291 }
    292 
    293 int
    294 oboe_close(void *h, int flag, int mode, struct proc *p)
    295 {
    296 	struct oboe_softc *sc = h;
    297 	int error = 0;
    298 	int s = splir();
    299 
    300 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    301 	/* Wait for output to drain */
    302 
    303 	if (sc->sc_txpending > 0) {
    304 		sc->sc_state |= OBOE_CLOSING;
    305 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
    306 	}
    307 	splx(s);
    308 
    309 	oboe_stopchip(sc);
    310 	return (error);
    311 }
    312 
    313 int
    314 oboe_read(void *h, struct uio *uio, int flag)
    315 {
    316 	struct oboe_softc *sc = h;
    317 	int error = 0;
    318 	int s;
    319 	int slot;
    320 
    321 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    322 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    323 		 (long)uio->uio_offset));
    324 
    325 	s = splir();
    326 	while (sc->sc_saved == 0) {
    327 		if (flag & IO_NDELAY) {
    328 			splx(s);
    329 			return (EWOULDBLOCK);
    330 		}
    331 		sc->sc_state |= OBOE_RSLP;
    332 		DPRINTF(("oboe_read: sleep\n"));
    333 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
    334 		DPRINTF(("oboe_read: woke, error=%d\n", error));
    335 		if (error) {
    336 			sc->sc_state &= ~OBOE_RSLP;
    337 			break;
    338 		}
    339 	}
    340 
    341 	/* Do just one frame transfer per read */
    342 
    343 	if (!error) {
    344 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
    345 		if (uio->uio_resid < sc->sc_lens[slot]) {
    346 			DPRINTF(("oboe_read: uio buffer smaller than frame size (%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 	/* 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 s = splir();
    423 
    424 	if (p->speed > 0) {
    425 		oboe_setbaud(sc, p->speed);
    426 	}
    427 	sc->sc_ebofs = p->ebofs;
    428 
    429 	splx(s);
    430 
    431 	/* 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 	int s = splir();
    440 	*speeds = sc->sc_speeds;
    441 	splx(s);
    442 	return (0);
    443 }
    444 
    445 int
    446 oboe_get_turnarounds(void *h, int *turnarounds)
    447 {
    448 #if 0
    449 	struct oboe_softc *sc = h;
    450 #endif
    451 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    452 	*turnarounds = 0x01; /* 10ms */
    453 	return (0);
    454 }
    455 
    456 int
    457 oboe_poll(void *h, int events, struct proc *p)
    458 {
    459 	struct oboe_softc *sc = h;
    460 	int revents = 0;
    461 	int s;
    462 
    463 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    464 
    465 	s = splir();
    466 	if (events & (POLLOUT | POLLWRNORM))
    467 		revents |= events & (POLLOUT | POLLWRNORM);
    468 	if (events & (POLLIN | POLLRDNORM)) {
    469 		if (sc->sc_saved > 0) {
    470 			DPRINTF(("%s: have data\n", __FUNCTION__));
    471 			revents |= events & (POLLIN | POLLRDNORM);
    472 		} else {
    473 			DPRINTF(("%s: recording select", __FUNCTION__));
    474 			selrecord(p, &sc->sc_rsel);
    475 		}
    476 	}
    477 	splx(s);
    478 
    479 	return (revents);
    480 }
    481 
    482 
    483 static int
    484 oboe_reset(struct oboe_softc *sc)
    485 {
    486 #if 0
    487 	OUTB(sc, 0x00, OBOE_RST);
    488 	OUTB(sc, 0x80, OBOE_RST);
    489 #endif
    490 	return 0;
    491 }
    492 
    493 static int
    494 oboe_intr(void *p)
    495 {
    496 	struct oboe_softc *sc = p;
    497 	uint8_t irqstat	= INB(sc, OBOE_ISR);
    498 
    499 	if (!(irqstat & 0xf8))
    500 		return (0); /* Not for me? */
    501 
    502 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
    503 
    504 	OUTB(sc, irqstat, OBOE_ISR);
    505 
    506 	if (irqstat & OBOE_ISR_RXDONE) {
    507 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
    508 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
    509 			if (sc->sc_saved == RX_SLOTS) {
    510 				DPRINTF(("oboe_intr: all buffers filled\n"));
    511 				return 0;
    512 			}
    513 
    514 			if (len > 2)
    515 				len -= 2; /* JSP: skip check sum? */
    516 
    517 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
    518 				 sc->sc_recv_stores[sc->sc_rxs]));
    519 			bcopy(sc->sc_recv_bufs[sc->sc_rxs],
    520 			      sc->sc_recv_stores[sc->sc_rxs],
    521 			      len);
    522 			sc->sc_lens[sc->sc_rxs] = len;
    523 			sc->sc_saved++;
    524 #if 0
    525 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
    526 				     len, &sc->sc_q);
    527 #endif
    528 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
    529 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
    530 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
    531 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
    532 		}
    533 		DPRINTF(("oboe_intr no more frames available\n"));
    534 		if (sc->sc_state & OBOE_RSLP) {
    535 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
    536 			sc->sc_state &= ~OBOE_RSLP;
    537 			DPRINTF(("oboe_intr: waking up reader\n"));
    538 			wakeup(&sc->sc_rxs);
    539 		}
    540 		selwakeup(&sc->sc_rsel);
    541 		DPRINTF(("oboe_intr returning\n"));
    542 	}
    543 	if (irqstat & OBOE_ISR_TXDONE) {
    544 	        DPRINTF(("oboe_intr: write done\n"));
    545 		sc->sc_txpending--;
    546 		sc->sc_txpackets++;
    547 
    548 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
    549 			wakeup(&sc->sc_state);
    550 			return 1;
    551 		}
    552 
    553 		if (sc->sc_state & OBOE_WSLP) {
    554 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
    555 			sc->sc_state &= ~OBOE_WSLP;
    556 			DPRINTF(("oboe_intr: waking up writer\n"));
    557 			wakeup(&sc->sc_txs);
    558 		}
    559 	}
    560 	return (1);
    561 }
    562 
    563 /* XXX vtophys must go! */
    564 static void
    565 oboe_init_taskfile(struct oboe_softc *sc)
    566 {
    567 	int i;
    568 	int s = splir();
    569 
    570 	for (i = 0; i < TX_SLOTS; ++i) {
    571 		sc->sc_taskfile->xmit[i].len = 0;
    572 		sc->sc_taskfile->xmit[i].control = 0x00;
    573 		sc->sc_taskfile->xmit[i].buffer =
    574 			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
    575 	}
    576 
    577 	for (i = 0; i < RX_SLOTS; ++i) {
    578 		sc->sc_taskfile->recv[i].len = 0;
    579 		sc->sc_taskfile->recv[i].control = 0x83;
    580 		sc->sc_taskfile->recv[i].buffer =
    581 			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
    582 	}
    583 
    584 	sc->sc_txpending = 0;
    585 
    586 	splx(s);
    587 }
    588 
    589 static int
    590 oboe_alloc_taskfile(struct oboe_softc *sc)
    591 {
    592 	int i;
    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 	uint32_t physaddr;
    631 
    632 	OUTB(sc, 0, OBOE_LOCK);
    633 	OUTB(sc, 0, OBOE_RST);
    634 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
    635 	OUTB(sc, 0xf0, OBOE_REG_D);
    636 	OUTB(sc, 0xff, OBOE_ISR);
    637 	OUTB(sc, 0x0f, OBOE_REG_1A);
    638 	OUTB(sc, 0xff, OBOE_REG_1B);
    639 
    640 	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
    641 
    642 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
    643 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
    644 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
    645 
    646 	OUTB(sc, 0x0e, OBOE_REG_11);
    647 	OUTB(sc, 0x80, OBOE_RST);
    648 
    649 	oboe_setbaud(sc, 9600);
    650 
    651 	sc->sc_rxs = INB(sc, OBOE_RCVT);
    652 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
    653 		sc->sc_rxs = 0;
    654 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
    655 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
    656 		sc->sc_txs = 0;
    657 }
    658 
    659 static void
    660 oboe_stopchip (struct oboe_softc *sc) {
    661 	OUTB(sc, 0x0e, OBOE_REG_11);
    662 	OUTB(sc, 0x00, OBOE_RST);
    663 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
    664 	OUTB(sc, 0xff, OBOE_TFP1);
    665 	OUTB(sc, 0xff, OBOE_TFP0);
    666 	OUTB(sc, 0x0f, OBOE_REG_1B);
    667 	OUTB(sc, 0xff, OBOE_REG_1A);
    668 	OUTB(sc, 0x00, OBOE_ISR); /* FIXME: should i do this to disable ints? */
    669 	OUTB(sc, 0x80, OBOE_RST);
    670 	OUTB(sc, 0x0e, OBOE_LOCK);
    671 }
    672 
    673 #define SPEEDCASE(speed, type, divisor) \
    674 case speed: \
    675 OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
    676 OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
    677 OUTB(sc, divisor, OBOE_UDIV); \
    678 break
    679 
    680 static void
    681 oboe_setbaud (struct oboe_softc *sc, int baud) {
    682 	int s;
    683 
    684 	DPRINTF(("oboe: setting baud to %d\n", baud));
    685 
    686 	s = splir();
    687 
    688 	switch (baud) {
    689 	SPEEDCASE(   2400, SIR, 0xbf);
    690 	SPEEDCASE(   9600, SIR, 0x2f);
    691 	SPEEDCASE(  19200, SIR, 0x17);
    692 	SPEEDCASE(  38400, SIR, 0x0b);
    693 	SPEEDCASE(  57600, SIR, 0x07);
    694 	SPEEDCASE( 115200, SIR, 0x03);
    695 	SPEEDCASE(1152000, MIR, 0x01);
    696 	SPEEDCASE(4000000, FIR, 0x00);
    697 	default:
    698 		printf("oboe: cannot set speed to %d\n", baud);
    699 		return;
    700 	}
    701 
    702 	splx(s);
    703 
    704 	OUTB(sc, 0x00, OBOE_RST);
    705 	OUTB(sc, 0x80, OBOE_RST);
    706 	OUTB(sc, 0x01, OBOE_REG_9);
    707 
    708 	sc->sc_speed = baud;
    709 }
    710