Home | History | Annotate | Line # | Download | only in pci
oboe.c revision 1.44
      1 /*	$NetBSD: oboe.c,v 1.44 2017/08/20 10:55:37 maxv 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  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Toshiba OBOE IrDA SIR/FIR driver.
     36  *
     37  * Based on information from the Linux driver, thus the magic hex numbers.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: oboe.c,v 1.44 2017/08/20 10:55:37 maxv Exp $");
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/device.h>
     47 #include <sys/malloc.h>
     48 #include <sys/tty.h>
     49 #include <sys/vnode.h>
     50 #include <sys/poll.h>
     51 #include <sys/proc.h>
     52 
     53 #include <dev/ir/ir.h>
     54 #include <dev/ir/irdaio.h>
     55 #include <dev/ir/irframevar.h>
     56 #include <dev/ir/sir.h>
     57 
     58 #include <dev/pci/pcidevs.h>
     59 #include <dev/pci/pcivar.h>
     60 
     61 #include <sys/bus.h>
     62 #include <sys/intr.h>
     63 
     64 #include <dev/pci/oboereg.h>
     65 
     66 static int oboe_match(device_t parent, cfdata_t match, void *aux);
     67 static void oboe_attach(device_t parent, device_t self, void *aux);
     68 static int oboe_detach(device_t self, int flags);
     69 
     70 static int oboe_open(void *h, int flag, int mode, struct lwp *l);
     71 static int oboe_close(void *h, int flag, int mode, struct lwp *l);
     72 static int oboe_read(void *h, struct uio *uio, int flag);
     73 static int oboe_write(void *h, struct uio *uio, int flag);
     74 static int oboe_set_params(void *h, struct irda_params *params);
     75 static int oboe_get_speeds(void *h, int *speeds);
     76 static int oboe_get_turnarounds(void *h, int *times);
     77 static int oboe_poll(void *h, int events, struct lwp *l);
     78 static int oboe_kqfilter(void *h, struct knote *kn);
     79 
     80 #ifdef OBOE_DEBUG
     81 #define DPRINTF(x)	if (oboedebug) printf x
     82 int oboedebug = 1;
     83 #else
     84 #define DPRINTF(x)
     85 #endif
     86 
     87 struct oboe_dma;
     88 
     89 struct oboe_softc {
     90 	device_t		sc_child;
     91 	struct pci_attach_args	sc_pa;
     92 	pci_intr_handle_t *	sc_ih;
     93 	unsigned int		sc_revision;	/* PCI Revision ID */
     94 	/* I/O Base device */
     95 	bus_space_tag_t		sc_iot;
     96 	bus_space_handle_t	sc_ioh;
     97 	bus_dma_tag_t		sc_dmatag;
     98 	struct selinfo		sc_rsel;
     99 	struct selinfo		sc_wsel;
    100 
    101 	int			sc_state;
    102 #define	OBOE_RSLP		0x01	/* waiting for data (read) */
    103 #define	OBOE_WSLP		0x02	/* waiting for data (write) */
    104 #define OBOE_CLOSING		0x04	/* waiting for output to drain */
    105 
    106 	int			sc_speeds;
    107 	int			sc_flags;
    108 	int			sc_speed;
    109 	int			sc_ebofs;
    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 	void *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 int oboe_setbaud(struct oboe_softc *, int);
    151 
    152 CFATTACH_DECL_NEW(oboe, sizeof(struct oboe_softc),
    153     oboe_match, oboe_attach, oboe_detach, NULL);
    154 
    155 static struct irframe_methods oboe_methods = {
    156 	oboe_open, oboe_close, oboe_read, oboe_write, oboe_poll,
    157 	oboe_kqfilter, oboe_set_params, oboe_get_speeds, oboe_get_turnarounds
    158 };
    159 
    160 static int
    161 oboe_match(device_t parent, cfdata_t match, void *aux)
    162 {
    163 	struct pci_attach_args *pa = aux;
    164 
    165 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
    166 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE ||
    167 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_DONAUOBOE))
    168 		return (1);
    169 	return 0;
    170 }
    171 
    172 static void
    173 oboe_attach(device_t parent, device_t self, void *aux)
    174 {
    175 	struct oboe_softc *sc = device_private(self);
    176 	struct pci_attach_args *pa = aux;
    177 	pci_intr_handle_t ih;
    178 	struct ir_attach_args ia;
    179 	const char *intrstring;
    180 	char intrbuf[PCI_INTRSTR_LEN];
    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 		aprint_error_dev(self, "can't map I/O space\n");
    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 		aprint_error_dev(self, "can't reset\n");
    210 		return;
    211 	}
    212 	/* Map and establish the interrupt. */
    213 	if (pci_intr_map(pa, &ih)) {
    214 		aprint_error_dev(self, "couldn't map interrupt\n");
    215 		return;
    216 	}
    217 	intrstring = pci_intr_string(pa->pa_pc, ih, intrbuf, sizeof(intrbuf));
    218 	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
    219 	if (sc->sc_ih == NULL) {
    220 		aprint_error_dev(self, "couldn't establish interrupt");
    221 		if (intrstring != NULL)
    222 			aprint_error(" at %s", intrstring);
    223 		aprint_error("\n");
    224 		return;
    225 	}
    226 	aprint_normal_dev(self, "interrupting at %s\n", intrstring);
    227 
    228 	selinit(&sc->sc_rsel);
    229 	selinit(&sc->sc_wsel);
    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(self, &ia, ir_print);
    242 }
    243 
    244 static int
    245 oboe_detach(device_t self, int flags)
    246 {
    247 	struct oboe_softc *sc = device_private(self);
    248 
    249 #ifdef OBOE_DEBUG
    250 	/* XXX needs reference counting for proper detach. */
    251 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    252 #endif
    253 	seldestroy(&sc->sc_rsel);
    254 	seldestroy(&sc->sc_wsel);
    255 	return (0);
    256 }
    257 
    258 static int
    259 oboe_open(void *h, int flag, int mode, struct lwp *l)
    260 {
    261 	struct oboe_softc *sc = h;
    262 
    263 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    264 
    265 	sc->sc_state = 0;
    266 	sc->sc_saved = 0;
    267 	oboe_init_taskfile(sc);
    268 	oboe_startchip(sc);
    269 
    270 	return (0);
    271 }
    272 
    273 static int
    274 oboe_close(void *h, int flag, int mode,
    275     struct lwp *l)
    276 {
    277 	struct oboe_softc *sc = h;
    278 	int error = 0;
    279 	int s = splir();
    280 
    281 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    282 	/* Wait for output to drain */
    283 
    284 	if (sc->sc_txpending > 0) {
    285 		sc->sc_state |= OBOE_CLOSING;
    286 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
    287 	}
    288 	splx(s);
    289 
    290 	oboe_stopchip(sc);
    291 	return (error);
    292 }
    293 
    294 static int
    295 oboe_read(void *h, struct uio *uio, int flag)
    296 {
    297 	struct oboe_softc *sc = h;
    298 	int error = 0;
    299 	int s;
    300 	int slot;
    301 
    302 	DPRINTF(("%s: resid=%zu, iovcnt=%d, offset=%jd\n",
    303 		 __func__, uio->uio_resid, uio->uio_iovcnt,
    304 		 (intmax_t)uio->uio_offset));
    305 
    306 	s = splir();
    307 	while (sc->sc_saved == 0) {
    308 		if (flag & IO_NDELAY) {
    309 			splx(s);
    310 			return (EWOULDBLOCK);
    311 		}
    312 		sc->sc_state |= OBOE_RSLP;
    313 		DPRINTF(("oboe_read: sleep\n"));
    314 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
    315 		DPRINTF(("oboe_read: woke, error=%d\n", error));
    316 		if (error) {
    317 			sc->sc_state &= ~OBOE_RSLP;
    318 			break;
    319 		}
    320 	}
    321 
    322 	/* Do just one frame transfer per read */
    323 
    324 	if (!error) {
    325 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
    326 		if (uio->uio_resid < sc->sc_lens[slot]) {
    327 			DPRINTF(("oboe_read: uio buffer smaller than frame size"
    328 			    "(%zu < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
    329 			error = EINVAL;
    330 		} else {
    331 			DPRINTF(("oboe_read: moving %d bytes from %p\n",
    332 				 sc->sc_lens[slot],
    333 				 sc->sc_recv_stores[slot]));
    334 			error = uiomove(sc->sc_recv_stores[slot],
    335 					sc->sc_lens[slot], uio);
    336 		}
    337 	}
    338 	sc->sc_saved--;
    339 	splx(s);
    340 
    341 	return (error);
    342 }
    343 
    344 static int
    345 oboe_write(void *h, struct uio *uio, int flag)
    346 {
    347 	struct oboe_softc *sc = h;
    348 	int error = 0;
    349 	int n;
    350 	int s = splir();
    351 
    352 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    353 	while (sc->sc_txpending == TX_SLOTS) {
    354 		if (flag & IO_NDELAY) {
    355 			splx(s);
    356 			return (EWOULDBLOCK);
    357 		}
    358 		sc->sc_state |= OBOE_WSLP;
    359 		DPRINTF(("oboe_write: sleep\n"));
    360 		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
    361 		DPRINTF(("oboe_write: woke up, error=%d\n", error));
    362 		if (error) {
    363 			sc->sc_state &= ~OBOE_WSLP;
    364 			break;
    365 		}
    366 	}
    367 	if (error)
    368 		goto err;
    369 	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
    370 		DPRINTF(("oboe_write: slot overrun\n"));
    371 	}
    372 
    373 	n = irda_sir_frame(sc->sc_xmit_bufs[sc->sc_txs], TX_BUF_SZ, uio,
    374 			   sc->sc_ebofs);
    375 	if (n < 0) {
    376 		error = -n;
    377 		goto err;
    378 	}
    379 	sc->sc_taskfile->xmit[sc->sc_txs].len = n;
    380 
    381 	OUTB(sc, 0, OBOE_RST);
    382 	OUTB(sc, 0x1e, OBOE_REG_11);
    383 
    384 	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
    385 
    386 	/* XXX Need delay here??? */
    387 	delay(1000);
    388 
    389 	sc->sc_txpending++;
    390 	OUTB(sc, 0x80, OBOE_RST);
    391 	OUTB(sc, 1, OBOE_REG_9);
    392 	sc->sc_txs++;
    393 	sc->sc_txs %= TX_SLOTS;
    394 
    395  err:
    396 	splx(s);
    397 	return (error);
    398 }
    399 
    400 static int
    401 oboe_set_params(void *h, struct irda_params *p)
    402 {
    403 	struct oboe_softc *sc = h;
    404 	int error;
    405 
    406 	if (p->speed > 0) {
    407 		error = oboe_setbaud(sc, p->speed);
    408 		if (error)
    409 			return (error);
    410 	}
    411 	sc->sc_ebofs = p->ebofs;
    412 
    413 	/* XXX ignore ebofs and maxsize for now */
    414 	return (0);
    415 }
    416 
    417 static int
    418 oboe_get_speeds(void *h, int *speeds)
    419 {
    420 	struct oboe_softc *sc = h;
    421 	*speeds = sc->sc_speeds;
    422 	return (0);
    423 }
    424 
    425 static int
    426 oboe_get_turnarounds(void *h, int *turnarounds)
    427 {
    428 #ifdef OBOE_DEBUG
    429 	struct oboe_softc *sc = h;
    430 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    431 #endif
    432 
    433 	/* XXX Linux driver sets all bits */
    434 	*turnarounds = IRDA_TURNT_10000; /* 10ms */
    435 
    436 	return (0);
    437 }
    438 
    439 static int
    440 oboe_poll(void *h, int events, struct lwp *l)
    441 {
    442 	struct oboe_softc *sc = h;
    443 	int revents = 0;
    444 	int s;
    445 
    446 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    447 
    448 	s = splir();
    449 	if (events & (POLLOUT | POLLWRNORM))
    450 		revents |= events & (POLLOUT | POLLWRNORM);
    451 	if (events & (POLLIN | POLLRDNORM)) {
    452 		if (sc->sc_saved > 0) {
    453 			DPRINTF(("%s: have data\n", __func__));
    454 			revents |= events & (POLLIN | POLLRDNORM);
    455 		} else {
    456 			DPRINTF(("%s: recording select\n", __func__));
    457 			selrecord(l, &sc->sc_rsel);
    458 		}
    459 	}
    460 	splx(s);
    461 
    462 	return (revents);
    463 }
    464 
    465 static void
    466 filt_oboerdetach(struct knote *kn)
    467 {
    468 	struct oboe_softc *sc = kn->kn_hook;
    469 	int s;
    470 
    471 	s = splir();
    472 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    473 	splx(s);
    474 }
    475 
    476 static int
    477 filt_oboeread(struct knote *kn, long hint)
    478 {
    479 	struct oboe_softc *sc = kn->kn_hook;
    480 
    481 	kn->kn_data = sc->sc_saved;
    482 	return (kn->kn_data > 0);
    483 }
    484 
    485 static void
    486 filt_oboewdetach(struct knote *kn)
    487 {
    488 	struct oboe_softc *sc = kn->kn_hook;
    489 	int s;
    490 
    491 	s = splir();
    492 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
    493 	splx(s);
    494 }
    495 
    496 static const struct filterops oboeread_filtops =
    497 	{ 1, NULL, filt_oboerdetach, filt_oboeread };
    498 static const struct filterops oboewrite_filtops =
    499 	{ 1, NULL, filt_oboewdetach, filt_seltrue };
    500 
    501 static int
    502 oboe_kqfilter(void *h, struct knote *kn)
    503 {
    504 	struct oboe_softc *sc = h;
    505 	struct klist *klist;
    506 	int s;
    507 
    508 	switch (kn->kn_filter) {
    509 	case EVFILT_READ:
    510 		klist = &sc->sc_rsel.sel_klist;
    511 		kn->kn_fop = &oboeread_filtops;
    512 		break;
    513 	case EVFILT_WRITE:
    514 		klist = &sc->sc_wsel.sel_klist;
    515 		kn->kn_fop = &oboewrite_filtops;
    516 		break;
    517 	default:
    518 		return (EINVAL);
    519 	}
    520 
    521 	kn->kn_hook = sc;
    522 
    523 	s = splir();
    524 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    525 	splx(s);
    526 
    527 	return (0);
    528 }
    529 
    530 static int
    531 oboe_reset(struct oboe_softc *sc)
    532 {
    533 #if 0
    534 	OUTB(sc, 0x00, OBOE_RST);
    535 	OUTB(sc, 0x80, OBOE_RST);
    536 #endif
    537 	return 0;
    538 }
    539 
    540 static int
    541 oboe_intr(void *p)
    542 {
    543 	struct oboe_softc *sc = p;
    544 	uint8_t irqstat	= INB(sc, OBOE_ISR);
    545 
    546 	if (!(irqstat & 0xf8))
    547 		return (0); /* Not for me? */
    548 
    549 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
    550 
    551 	OUTB(sc, irqstat, OBOE_ISR);
    552 
    553 	if (irqstat & OBOE_ISR_RXDONE) {
    554 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
    555 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
    556 			if (sc->sc_saved == RX_SLOTS) {
    557 				DPRINTF(("oboe_intr: all buffers filled\n"));
    558 				return 0;
    559 			}
    560 
    561 			if (len > 2)
    562 				len -= 2; /* JSP: skip check sum? */
    563 
    564 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
    565 				 sc->sc_recv_stores[sc->sc_rxs]));
    566 			memcpy(sc->sc_recv_stores[sc->sc_rxs],
    567 			       sc->sc_recv_bufs[sc->sc_rxs],
    568 			       len);
    569 			sc->sc_lens[sc->sc_rxs] = len;
    570 			sc->sc_saved++;
    571 #if 0
    572 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
    573 				     len, &sc->sc_q);
    574 #endif
    575 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
    576 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
    577 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
    578 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
    579 		}
    580 		DPRINTF(("oboe_intr no more frames available\n"));
    581 		if (sc->sc_state & OBOE_RSLP) {
    582 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
    583 			sc->sc_state &= ~OBOE_RSLP;
    584 			DPRINTF(("oboe_intr: waking up reader\n"));
    585 			wakeup(&sc->sc_rxs);
    586 		}
    587 		selnotify(&sc->sc_rsel, 0, 0);
    588 		DPRINTF(("oboe_intr returning\n"));
    589 	}
    590 	if (irqstat & OBOE_ISR_TXDONE) {
    591 	        DPRINTF(("oboe_intr: write done\n"));
    592 		sc->sc_txpending--;
    593 		sc->sc_txpackets++;
    594 
    595 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
    596 			wakeup(&sc->sc_state);
    597 			return 1;
    598 		}
    599 
    600 		if (sc->sc_state & OBOE_WSLP) {
    601 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
    602 			sc->sc_state &= ~OBOE_WSLP;
    603 			DPRINTF(("oboe_intr: waking up writer\n"));
    604 			wakeup(&sc->sc_txs);
    605 		}
    606 		selnotify(&sc->sc_wsel, 0, 0);
    607 	}
    608 	return (1);
    609 }
    610 
    611 /* XXX vtophys must go! */
    612 static void
    613 oboe_init_taskfile(struct oboe_softc *sc)
    614 {
    615 	int i;
    616 	int s = splir();
    617 
    618 	for (i = 0; i < TX_SLOTS; ++i) {
    619 		sc->sc_taskfile->xmit[i].len = 0;
    620 		sc->sc_taskfile->xmit[i].control = 0x00;
    621 		sc->sc_taskfile->xmit[i].buffer =
    622 		    vtophys((uintptr_t)sc->sc_xmit_bufs[i]);
    623 	}
    624 
    625 	for (i = 0; i < RX_SLOTS; ++i) {
    626 		sc->sc_taskfile->recv[i].len = 0;
    627 		sc->sc_taskfile->recv[i].control = 0x83;
    628 		sc->sc_taskfile->recv[i].buffer =
    629 		    vtophys((uintptr_t)sc->sc_recv_bufs[i]);
    630 	}
    631 
    632 	sc->sc_txpending = 0;
    633 
    634 	splx(s);
    635 }
    636 
    637 static int
    638 oboe_alloc_taskfile(struct oboe_softc *sc)
    639 {
    640 	int i;
    641 	/* XXX */
    642 	uintptr_t addr =
    643 	    (uintptr_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
    644 
    645 	addr &= ~(sizeof (struct OboeTaskFile) - 1);
    646 	addr += sizeof (struct OboeTaskFile);
    647 	sc->sc_taskfile = (struct OboeTaskFile *) addr;
    648 
    649 	for (i = 0; i < TX_SLOTS; ++i) {
    650 		sc->sc_xmit_bufs[i] =
    651 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    652 		sc->sc_xmit_stores[i] =
    653 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    654 	}
    655 	for (i = 0; i < RX_SLOTS; ++i) {
    656 		sc->sc_recv_bufs[i] =
    657 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    658 		sc->sc_recv_stores[i] =
    659 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    660 	}
    661 
    662 	return 0;
    663 }
    664 
    665 static void
    666 oboe_startchip (struct oboe_softc *sc)
    667 {
    668 	uint32_t physaddr;
    669 
    670 	OUTB(sc, 0, OBOE_LOCK);
    671 	OUTB(sc, 0, OBOE_RST);
    672 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
    673 	OUTB(sc, 0xf0, OBOE_REG_D);
    674 	OUTB(sc, 0xff, OBOE_ISR);
    675 	OUTB(sc, 0x0f, OBOE_REG_1A);
    676 	OUTB(sc, 0xff, OBOE_REG_1B);
    677 
    678 	physaddr = vtophys((uintptr_t)sc->sc_taskfile);
    679 
    680 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
    681 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
    682 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
    683 
    684 	OUTB(sc, 0x0e, OBOE_REG_11);
    685 	OUTB(sc, 0x80, OBOE_RST);
    686 
    687 	(void)oboe_setbaud(sc, 9600);
    688 
    689 	sc->sc_rxs = INB(sc, OBOE_RCVT);
    690 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
    691 		sc->sc_rxs = 0;
    692 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
    693 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
    694 		sc->sc_txs = 0;
    695 }
    696 
    697 static void
    698 oboe_stopchip (struct oboe_softc *sc)
    699 {
    700 	OUTB(sc, 0x0e, OBOE_REG_11);
    701 	OUTB(sc, 0x00, OBOE_RST);
    702 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
    703 	OUTB(sc, 0xff, OBOE_TFP1);
    704 	OUTB(sc, 0xff, OBOE_TFP0);
    705 	OUTB(sc, 0x0f, OBOE_REG_1B);
    706 	OUTB(sc, 0xff, OBOE_REG_1A);
    707 	OUTB(sc, 0x00, OBOE_ISR); /* XXX: should i do this to disable ints? */
    708 	OUTB(sc, 0x80, OBOE_RST);
    709 	OUTB(sc, 0x0e, OBOE_LOCK);
    710 }
    711 
    712 #define SPEEDCASE(speed, type, divisor) \
    713 case speed: \
    714 OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
    715 OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
    716 OUTB(sc, divisor, OBOE_UDIV); \
    717 break
    718 
    719 static int
    720 oboe_setbaud(struct oboe_softc *sc, int baud)
    721 {
    722 	int s;
    723 
    724 	DPRINTF(("oboe: setting baud to %d\n", baud));
    725 
    726 	s = splir();
    727 
    728 	switch (baud) {
    729 	SPEEDCASE(   2400, SIR, 0xbf);
    730 	SPEEDCASE(   9600, SIR, 0x2f);
    731 	SPEEDCASE(  19200, SIR, 0x17);
    732 	SPEEDCASE(  38400, SIR, 0x0b);
    733 	SPEEDCASE(  57600, SIR, 0x07);
    734 	SPEEDCASE( 115200, SIR, 0x03);
    735 	SPEEDCASE(1152000, MIR, 0x01);
    736 	SPEEDCASE(4000000, FIR, 0x00);
    737 	default:
    738 		DPRINTF(("oboe: cannot set speed to %d\n", baud));
    739 		splx(s);
    740 		return (EINVAL);
    741 	}
    742 
    743 	OUTB(sc, 0x00, OBOE_RST);
    744 	OUTB(sc, 0x80, OBOE_RST);
    745 	OUTB(sc, 0x01, OBOE_REG_9);
    746 
    747 	sc->sc_speed = baud;
    748 
    749 	splx(s);
    750 
    751 	return (0);
    752 }
    753