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