Home | History | Annotate | Line # | Download | only in pci
oboe.c revision 1.22
      1 /*	$NetBSD: oboe.c,v 1.22 2006/08/17 22:14:38 he 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.22 2006/08/17 22:14:38 he Exp $");
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/kernel.h>
     53 #include <sys/device.h>
     54 #include <sys/malloc.h>
     55 #include <sys/tty.h>
     56 #include <sys/vnode.h>
     57 #include <sys/poll.h>
     58 
     59 #include <dev/ir/ir.h>
     60 #include <dev/ir/irdaio.h>
     61 #include <dev/ir/irframevar.h>
     62 #include <dev/ir/sir.h>
     63 
     64 #include <dev/pci/pcidevs.h>
     65 #include <dev/pci/pcivar.h>
     66 
     67 #include <machine/bus.h>
     68 #include <machine/intr.h>
     69 #include <uvm/uvm_extern.h>
     70 
     71 #include <dev/pci/oboereg.h>
     72 
     73 static int oboe_match(struct device *parent, struct cfdata *match, void *aux);
     74 static void oboe_attach(struct device *parent, struct device *self, void *aux);
     75 static int oboe_activate(struct device *self, enum devact act);
     76 static int oboe_detach(struct device *self, int flags);
     77 
     78 static int oboe_open(void *h, int flag, int mode, struct lwp *l);
     79 static int oboe_close(void *h, int flag, int mode, struct lwp *l);
     80 static int oboe_read(void *h, struct uio *uio, int flag);
     81 static int oboe_write(void *h, struct uio *uio, int flag);
     82 static int oboe_set_params(void *h, struct irda_params *params);
     83 static int oboe_get_speeds(void *h, int *speeds);
     84 static int oboe_get_turnarounds(void *h, int *times);
     85 static int oboe_poll(void *h, int events, struct lwp *l);
     86 static int oboe_kqfilter(void *h, struct knote *kn);
     87 
     88 #ifdef OBOE_DEBUG
     89 #define DPRINTF(x)	if (oboedebug) printf x
     90 int oboedebug = 1;
     91 #else
     92 #define DPRINTF(x)
     93 #endif
     94 
     95 struct oboe_dma;
     96 
     97 struct oboe_softc {
     98 	struct device		sc_dev;
     99 	struct device		*sc_child;
    100 	struct pci_attach_args	sc_pa;
    101 	pci_intr_handle_t *	sc_ih;
    102 	unsigned int		sc_revision;	/* PCI Revision ID */
    103 	/* I/O Base device */
    104 	bus_space_tag_t		sc_iot;
    105 	bus_space_handle_t	sc_ioh;
    106 	bus_dma_tag_t		sc_dmatag;
    107 	struct selinfo		sc_rsel;
    108 	struct selinfo		sc_wsel;
    109 
    110 	int			sc_state;
    111 #define	OBOE_RSLP		0x01	/* waiting for data (read) */
    112 #define	OBOE_WSLP		0x02	/* waiting for data (write) */
    113 #define OBOE_CLOSING		0x04	/* waiting for output to drain */
    114 
    115 	int			sc_speeds;
    116 	int			sc_flags;
    117 	int			sc_speed;
    118 	int			sc_ebofs;
    119 
    120 	struct oboe_dma		*sc_dmas;
    121 	struct OboeTaskFile	*sc_taskfile;    /* The taskfile   */
    122 	u_char *		sc_xmit_bufs[TX_SLOTS];
    123 	u_char *		sc_recv_bufs[RX_SLOTS];
    124 	void *			sc_xmit_stores[TX_SLOTS];
    125 	void *			sc_recv_stores[RX_SLOTS];
    126 	int			sc_txs; /* Current transmit slot number */
    127 	int			sc_rxs; /* Current receive slot number */
    128 	int			sc_saved; /* number of saved frames */
    129 	int			sc_lens[RX_SLOTS];
    130 
    131 	int			sc_txpending;
    132 
    133 	/* Statistics */
    134 	int			sc_txpackets;
    135 	int			sc_rxpackets;
    136 	int			sc_txerrors;
    137 	int			sc_rxerrors;
    138 };
    139 
    140 static int oboe_intr(void *handle);
    141 static int oboe_reset(struct oboe_softc *);
    142 
    143 struct oboe_dma {
    144 	bus_dmamap_t map;
    145 	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 static struct irframe_methods oboe_methods = {
    165 	oboe_open, oboe_close, oboe_read, oboe_write, oboe_poll,
    166 	oboe_kqfilter, oboe_set_params, oboe_get_speeds, oboe_get_turnarounds
    167 };
    168 
    169 static int
    170 oboe_match(struct device *parent, struct cfdata *match, 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 static 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 static int
    251 oboe_activate(struct device *self, enum devact act)
    252 {
    253 	struct oboe_softc *sc = (struct oboe_softc *)self;
    254 	int error = 0;
    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 		break;
    267 	}
    268 	return (error);
    269 }
    270 
    271 static int
    272 oboe_detach(struct device *self, int flags)
    273 {
    274 #ifdef OBOE_DEBUG
    275 	struct oboe_softc *sc = (struct oboe_softc *)self;
    276 
    277 	/* XXX needs reference counting for proper detach. */
    278 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    279 #endif
    280 	return (0);
    281 }
    282 
    283 static int
    284 oboe_open(void *h, int flag, int mode, struct lwp *l)
    285 {
    286 	struct oboe_softc *sc = h;
    287 
    288 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    289 
    290 	sc->sc_state = 0;
    291 	sc->sc_saved = 0;
    292 	oboe_init_taskfile(sc);
    293 	oboe_startchip(sc);
    294 
    295 	return (0);
    296 }
    297 
    298 static int
    299 oboe_close(void *h, int flag, int mode, struct lwp *l)
    300 {
    301 	struct oboe_softc *sc = h;
    302 	int error = 0;
    303 	int s = splir();
    304 
    305 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    306 	/* Wait for output to drain */
    307 
    308 	if (sc->sc_txpending > 0) {
    309 		sc->sc_state |= OBOE_CLOSING;
    310 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
    311 	}
    312 	splx(s);
    313 
    314 	oboe_stopchip(sc);
    315 	return (error);
    316 }
    317 
    318 static int
    319 oboe_read(void *h, struct uio *uio, int flag)
    320 {
    321 	struct oboe_softc *sc = h;
    322 	int error = 0;
    323 	int s;
    324 	int slot;
    325 
    326 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    327 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    328 		 (long)uio->uio_offset));
    329 
    330 	s = splir();
    331 	while (sc->sc_saved == 0) {
    332 		if (flag & IO_NDELAY) {
    333 			splx(s);
    334 			return (EWOULDBLOCK);
    335 		}
    336 		sc->sc_state |= OBOE_RSLP;
    337 		DPRINTF(("oboe_read: sleep\n"));
    338 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
    339 		DPRINTF(("oboe_read: woke, error=%d\n", error));
    340 		if (error) {
    341 			sc->sc_state &= ~OBOE_RSLP;
    342 			break;
    343 		}
    344 	}
    345 
    346 	/* Do just one frame transfer per read */
    347 
    348 	if (!error) {
    349 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
    350 		if (uio->uio_resid < sc->sc_lens[slot]) {
    351 			DPRINTF(("oboe_read: uio buffer smaller than frame size"
    352 			    "(%d < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
    353 			error = EINVAL;
    354 		} else {
    355 			DPRINTF(("oboe_read: moving %d bytes from %p\n",
    356 				 sc->sc_lens[slot],
    357 				 sc->sc_recv_stores[slot]));
    358 			error = uiomove(sc->sc_recv_stores[slot],
    359 					sc->sc_lens[slot], uio);
    360 		}
    361 	}
    362 	sc->sc_saved--;
    363 	splx(s);
    364 
    365 	return (error);
    366 }
    367 
    368 static int
    369 oboe_write(void *h, struct uio *uio, int flag)
    370 {
    371 	struct oboe_softc *sc = h;
    372 	int error = 0;
    373 	int n;
    374 	int s = splir();
    375 
    376 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    377 	while (sc->sc_txpending == TX_SLOTS) {
    378 		if (flag & IO_NDELAY) {
    379 			splx(s);
    380 			return (EWOULDBLOCK);
    381 		}
    382 		sc->sc_state |= OBOE_WSLP;
    383 		DPRINTF(("oboe_write: sleep\n"));
    384 		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
    385 		DPRINTF(("oboe_write: woke up, error=%d\n", error));
    386 		if (error) {
    387 			sc->sc_state &= ~OBOE_WSLP;
    388 			break;
    389 		}
    390 	}
    391 	if (error)
    392 		goto err;
    393 	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
    394 		DPRINTF(("oboe_write: slot overrun\n"));
    395 	}
    396 
    397 	n = irda_sir_frame(sc->sc_xmit_bufs[sc->sc_txs], TX_BUF_SZ, uio,
    398 			   sc->sc_ebofs);
    399 	if (n < 0) {
    400 		error = -n;
    401 		goto err;
    402 	}
    403 	sc->sc_taskfile->xmit[sc->sc_txs].len = n;
    404 
    405 	OUTB(sc, 0, OBOE_RST);
    406 	OUTB(sc, 0x1e, OBOE_REG_11);
    407 
    408 	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
    409 
    410 	/* XXX Need delay here??? */
    411 	delay(1000);
    412 
    413 	sc->sc_txpending++;
    414 	OUTB(sc, 0x80, OBOE_RST);
    415 	OUTB(sc, 1, OBOE_REG_9);
    416 	sc->sc_txs++;
    417 	sc->sc_txs %= TX_SLOTS;
    418 
    419  err:
    420 	splx(s);
    421 	return (error);
    422 }
    423 
    424 static int
    425 oboe_set_params(void *h, struct irda_params *p)
    426 {
    427 	struct oboe_softc *sc = h;
    428 	int error;
    429 
    430 	if (p->speed > 0) {
    431 		error = oboe_setbaud(sc, p->speed);
    432 		if (error)
    433 			return (error);
    434 	}
    435 	sc->sc_ebofs = p->ebofs;
    436 
    437 	/* XXX ignore ebofs and maxsize for now */
    438 	return (0);
    439 }
    440 
    441 static int
    442 oboe_get_speeds(void *h, int *speeds)
    443 {
    444 	struct oboe_softc *sc = h;
    445 	*speeds = sc->sc_speeds;
    446 	return (0);
    447 }
    448 
    449 static int
    450 oboe_get_turnarounds(void *h, int *turnarounds)
    451 {
    452 #ifdef OBOE_DEBUG
    453 	struct oboe_softc *sc = h;
    454 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    455 #endif
    456 
    457 	/* XXX Linux driver sets all bits */
    458 	*turnarounds = IRDA_TURNT_10000; /* 10ms */
    459 
    460 	return (0);
    461 }
    462 
    463 static int
    464 oboe_poll(void *h, int events, struct lwp *l)
    465 {
    466 	struct oboe_softc *sc = h;
    467 	int revents = 0;
    468 	int s;
    469 
    470 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    471 
    472 	s = splir();
    473 	if (events & (POLLOUT | POLLWRNORM))
    474 		revents |= events & (POLLOUT | POLLWRNORM);
    475 	if (events & (POLLIN | POLLRDNORM)) {
    476 		if (sc->sc_saved > 0) {
    477 			DPRINTF(("%s: have data\n", __FUNCTION__));
    478 			revents |= events & (POLLIN | POLLRDNORM);
    479 		} else {
    480 			DPRINTF(("%s: recording select\n", __FUNCTION__));
    481 			selrecord(l, &sc->sc_rsel);
    482 		}
    483 	}
    484 	splx(s);
    485 
    486 	return (revents);
    487 }
    488 
    489 static void
    490 filt_oboerdetach(struct knote *kn)
    491 {
    492 	struct oboe_softc *sc = kn->kn_hook;
    493 	int s;
    494 
    495 	s = splir();
    496 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    497 	splx(s);
    498 }
    499 
    500 static int
    501 filt_oboeread(struct knote *kn, long hint)
    502 {
    503 	struct oboe_softc *sc = kn->kn_hook;
    504 
    505 	kn->kn_data = sc->sc_saved;
    506 	return (kn->kn_data > 0);
    507 }
    508 
    509 static void
    510 filt_oboewdetach(struct knote *kn)
    511 {
    512 	struct oboe_softc *sc = kn->kn_hook;
    513 	int s;
    514 
    515 	s = splir();
    516 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
    517 	splx(s);
    518 }
    519 
    520 static const struct filterops oboeread_filtops =
    521 	{ 1, NULL, filt_oboerdetach, filt_oboeread };
    522 static const struct filterops oboewrite_filtops =
    523 	{ 1, NULL, filt_oboewdetach, filt_seltrue };
    524 
    525 static int
    526 oboe_kqfilter(void *h, struct knote *kn)
    527 {
    528 	struct oboe_softc *sc = h;
    529 	struct klist *klist;
    530 	int s;
    531 
    532 	switch (kn->kn_filter) {
    533 	case EVFILT_READ:
    534 		klist = &sc->sc_rsel.sel_klist;
    535 		kn->kn_fop = &oboeread_filtops;
    536 		break;
    537 	case EVFILT_WRITE:
    538 		klist = &sc->sc_wsel.sel_klist;
    539 		kn->kn_fop = &oboewrite_filtops;
    540 		break;
    541 	default:
    542 		return (1);
    543 	}
    544 
    545 	kn->kn_hook = sc;
    546 
    547 	s = splir();
    548 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    549 	splx(s);
    550 
    551 	return (0);
    552 }
    553 
    554 static int
    555 oboe_reset(struct oboe_softc *sc)
    556 {
    557 #if 0
    558 	OUTB(sc, 0x00, OBOE_RST);
    559 	OUTB(sc, 0x80, OBOE_RST);
    560 #endif
    561 	return 0;
    562 }
    563 
    564 static int
    565 oboe_intr(void *p)
    566 {
    567 	struct oboe_softc *sc = p;
    568 	uint8_t irqstat	= INB(sc, OBOE_ISR);
    569 
    570 	if (!(irqstat & 0xf8))
    571 		return (0); /* Not for me? */
    572 
    573 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
    574 
    575 	OUTB(sc, irqstat, OBOE_ISR);
    576 
    577 	if (irqstat & OBOE_ISR_RXDONE) {
    578 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
    579 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
    580 			if (sc->sc_saved == RX_SLOTS) {
    581 				DPRINTF(("oboe_intr: all buffers filled\n"));
    582 				return 0;
    583 			}
    584 
    585 			if (len > 2)
    586 				len -= 2; /* JSP: skip check sum? */
    587 
    588 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
    589 				 sc->sc_recv_stores[sc->sc_rxs]));
    590 			memcpy(sc->sc_recv_stores[sc->sc_rxs],
    591 			       sc->sc_recv_bufs[sc->sc_rxs],
    592 			       len);
    593 			sc->sc_lens[sc->sc_rxs] = len;
    594 			sc->sc_saved++;
    595 #if 0
    596 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
    597 				     len, &sc->sc_q);
    598 #endif
    599 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
    600 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
    601 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
    602 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
    603 		}
    604 		DPRINTF(("oboe_intr no more frames available\n"));
    605 		if (sc->sc_state & OBOE_RSLP) {
    606 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
    607 			sc->sc_state &= ~OBOE_RSLP;
    608 			DPRINTF(("oboe_intr: waking up reader\n"));
    609 			wakeup(&sc->sc_rxs);
    610 		}
    611 		selnotify(&sc->sc_rsel, 0);
    612 		DPRINTF(("oboe_intr returning\n"));
    613 	}
    614 	if (irqstat & OBOE_ISR_TXDONE) {
    615 	        DPRINTF(("oboe_intr: write done\n"));
    616 		sc->sc_txpending--;
    617 		sc->sc_txpackets++;
    618 
    619 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
    620 			wakeup(&sc->sc_state);
    621 			return 1;
    622 		}
    623 
    624 		if (sc->sc_state & OBOE_WSLP) {
    625 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
    626 			sc->sc_state &= ~OBOE_WSLP;
    627 			DPRINTF(("oboe_intr: waking up writer\n"));
    628 			wakeup(&sc->sc_txs);
    629 		}
    630 		selnotify(&sc->sc_wsel, 0);
    631 	}
    632 	return (1);
    633 }
    634 
    635 /* XXX vtophys must go! */
    636 static void
    637 oboe_init_taskfile(struct oboe_softc *sc)
    638 {
    639 	int i;
    640 	int s = splir();
    641 
    642 	for (i = 0; i < TX_SLOTS; ++i) {
    643 		sc->sc_taskfile->xmit[i].len = 0;
    644 		sc->sc_taskfile->xmit[i].control = 0x00;
    645 		sc->sc_taskfile->xmit[i].buffer =
    646 			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
    647 	}
    648 
    649 	for (i = 0; i < RX_SLOTS; ++i) {
    650 		sc->sc_taskfile->recv[i].len = 0;
    651 		sc->sc_taskfile->recv[i].control = 0x83;
    652 		sc->sc_taskfile->recv[i].buffer =
    653 			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
    654 	}
    655 
    656 	sc->sc_txpending = 0;
    657 
    658 	splx(s);
    659 }
    660 
    661 static int
    662 oboe_alloc_taskfile(struct oboe_softc *sc)
    663 {
    664 	int i;
    665 	/* XXX */
    666 	uint32_t addr = (uint32_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
    667 	if (addr == 0) {
    668 		goto bad;
    669 	}
    670 	addr &= ~(sizeof (struct OboeTaskFile) - 1);
    671 	addr += sizeof (struct OboeTaskFile);
    672 	sc->sc_taskfile = (struct OboeTaskFile *) addr;
    673 
    674 	for (i = 0; i < TX_SLOTS; ++i) {
    675 		sc->sc_xmit_bufs[i] =
    676 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    677 		sc->sc_xmit_stores[i] =
    678 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    679 		if (sc->sc_xmit_bufs[i] == NULL ||
    680 		    sc->sc_xmit_stores[i] == NULL) {
    681 			goto bad;
    682 		}
    683 	}
    684 	for (i = 0; i < RX_SLOTS; ++i) {
    685 		sc->sc_recv_bufs[i] =
    686 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    687 		sc->sc_recv_stores[i] =
    688 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    689 		if (sc->sc_recv_bufs[i] == NULL ||
    690 		    sc->sc_recv_stores[i] == NULL) {
    691 			goto bad;
    692 		}
    693 	}
    694 
    695 	return 0;
    696 bad:
    697 	printf("oboe: malloc for buffers failed()\n");
    698 	return 1;
    699 }
    700 
    701 static void
    702 oboe_startchip (struct oboe_softc *sc)
    703 {
    704 	uint32_t physaddr;
    705 
    706 	OUTB(sc, 0, OBOE_LOCK);
    707 	OUTB(sc, 0, OBOE_RST);
    708 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
    709 	OUTB(sc, 0xf0, OBOE_REG_D);
    710 	OUTB(sc, 0xff, OBOE_ISR);
    711 	OUTB(sc, 0x0f, OBOE_REG_1A);
    712 	OUTB(sc, 0xff, OBOE_REG_1B);
    713 
    714 	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
    715 
    716 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
    717 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
    718 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
    719 
    720 	OUTB(sc, 0x0e, OBOE_REG_11);
    721 	OUTB(sc, 0x80, OBOE_RST);
    722 
    723 	(void)oboe_setbaud(sc, 9600);
    724 
    725 	sc->sc_rxs = INB(sc, OBOE_RCVT);
    726 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
    727 		sc->sc_rxs = 0;
    728 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
    729 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
    730 		sc->sc_txs = 0;
    731 }
    732 
    733 static void
    734 oboe_stopchip (struct oboe_softc *sc)
    735 {
    736 	OUTB(sc, 0x0e, OBOE_REG_11);
    737 	OUTB(sc, 0x00, OBOE_RST);
    738 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
    739 	OUTB(sc, 0xff, OBOE_TFP1);
    740 	OUTB(sc, 0xff, OBOE_TFP0);
    741 	OUTB(sc, 0x0f, OBOE_REG_1B);
    742 	OUTB(sc, 0xff, OBOE_REG_1A);
    743 	OUTB(sc, 0x00, OBOE_ISR); /* XXX: should i do this to disable ints? */
    744 	OUTB(sc, 0x80, OBOE_RST);
    745 	OUTB(sc, 0x0e, OBOE_LOCK);
    746 }
    747 
    748 #define SPEEDCASE(speed, type, divisor) \
    749 case speed: \
    750 OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
    751 OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
    752 OUTB(sc, divisor, OBOE_UDIV); \
    753 break
    754 
    755 static int
    756 oboe_setbaud(struct oboe_softc *sc, int baud)
    757 {
    758 	int s;
    759 
    760 	DPRINTF(("oboe: setting baud to %d\n", baud));
    761 
    762 	s = splir();
    763 
    764 	switch (baud) {
    765 	SPEEDCASE(   2400, SIR, 0xbf);
    766 	SPEEDCASE(   9600, SIR, 0x2f);
    767 	SPEEDCASE(  19200, SIR, 0x17);
    768 	SPEEDCASE(  38400, SIR, 0x0b);
    769 	SPEEDCASE(  57600, SIR, 0x07);
    770 	SPEEDCASE( 115200, SIR, 0x03);
    771 	SPEEDCASE(1152000, MIR, 0x01);
    772 	SPEEDCASE(4000000, FIR, 0x00);
    773 	default:
    774 		DPRINTF(("oboe: cannot set speed to %d\n", baud));
    775 		splx(s);
    776 		return (EINVAL);
    777 	}
    778 
    779 	OUTB(sc, 0x00, OBOE_RST);
    780 	OUTB(sc, 0x80, OBOE_RST);
    781 	OUTB(sc, 0x01, OBOE_REG_9);
    782 
    783 	sc->sc_speed = baud;
    784 
    785 	splx(s);
    786 
    787 	return (0);
    788 }
    789