Home | History | Annotate | Line # | Download | only in pci
oboe.c revision 1.30
      1 /*	$NetBSD: oboe.c,v 1.30 2008/03/01 14:16:50 rmind 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.30 2008/03/01 14:16:50 rmind 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 		printf("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
    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 		printf("%s: can't reset\n", sc->sc_dev.dv_xname);
    220 		return;
    221 	}
    222 	/* Map and establish the interrupt. */
    223 	if (pci_intr_map(pa, &ih)) {
    224 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    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 		printf("%s: couldn't establish interrupt",
    231 		    sc->sc_dev.dv_xname);
    232 		if (intrstring != NULL)
    233 			printf(" at %s", intrstring);
    234 		printf("\n");
    235 		return;
    236 	}
    237 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstring);
    238 
    239 	selinit(&sc->sc_rsel);
    240 	selinit(&sc->sc_wsel);
    241 
    242 	sc->sc_txs = 0;
    243 	sc->sc_rxs = 0;
    244 
    245 	sc->sc_speeds =
    246 		IRDA_SPEED_2400   | IRDA_SPEED_9600    | IRDA_SPEED_19200 |
    247 		IRDA_SPEED_38400  | IRDA_SPEED_57600   | IRDA_SPEED_115200 |
    248 		IRDA_SPEED_576000 | IRDA_SPEED_1152000 | IRDA_SPEED_4000000;
    249 
    250 	oboe_alloc_taskfile(sc);
    251 
    252 	sc->sc_child = config_found((void *)sc, &ia, ir_print);
    253 }
    254 
    255 static int
    256 oboe_activate(struct device *self, enum devact act)
    257 {
    258 	struct oboe_softc *sc = (struct oboe_softc *)self;
    259 	int error = 0;
    260 
    261 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    262 
    263 	switch (act) {
    264 	case DVACT_ACTIVATE:
    265 		return (EOPNOTSUPP);
    266 		break;
    267 
    268 	case DVACT_DEACTIVATE:
    269 		if (sc->sc_child != NULL)
    270 			error = config_deactivate(sc->sc_child);
    271 		break;
    272 	}
    273 	return (error);
    274 }
    275 
    276 static int
    277 oboe_detach(struct device *self, int flags)
    278 {
    279 	struct oboe_softc *sc = (struct oboe_softc *)self;
    280 
    281 #ifdef OBOE_DEBUG
    282 	/* XXX needs reference counting for proper detach. */
    283 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    284 #endif
    285 	seldestroy(&sc->sc_rsel);
    286 	seldestroy(&sc->sc_wsel);
    287 	return (0);
    288 }
    289 
    290 static int
    291 oboe_open(void *h, int flag, int mode, struct lwp *l)
    292 {
    293 	struct oboe_softc *sc = h;
    294 
    295 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    296 
    297 	sc->sc_state = 0;
    298 	sc->sc_saved = 0;
    299 	oboe_init_taskfile(sc);
    300 	oboe_startchip(sc);
    301 
    302 	return (0);
    303 }
    304 
    305 static int
    306 oboe_close(void *h, int flag, int mode,
    307     struct lwp *l)
    308 {
    309 	struct oboe_softc *sc = h;
    310 	int error = 0;
    311 	int s = splir();
    312 
    313 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    314 	/* Wait for output to drain */
    315 
    316 	if (sc->sc_txpending > 0) {
    317 		sc->sc_state |= OBOE_CLOSING;
    318 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
    319 	}
    320 	splx(s);
    321 
    322 	oboe_stopchip(sc);
    323 	return (error);
    324 }
    325 
    326 static int
    327 oboe_read(void *h, struct uio *uio, int flag)
    328 {
    329 	struct oboe_softc *sc = h;
    330 	int error = 0;
    331 	int s;
    332 	int slot;
    333 
    334 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    335 		 __func__, uio->uio_resid, uio->uio_iovcnt,
    336 		 (long)uio->uio_offset));
    337 
    338 	s = splir();
    339 	while (sc->sc_saved == 0) {
    340 		if (flag & IO_NDELAY) {
    341 			splx(s);
    342 			return (EWOULDBLOCK);
    343 		}
    344 		sc->sc_state |= OBOE_RSLP;
    345 		DPRINTF(("oboe_read: sleep\n"));
    346 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
    347 		DPRINTF(("oboe_read: woke, error=%d\n", error));
    348 		if (error) {
    349 			sc->sc_state &= ~OBOE_RSLP;
    350 			break;
    351 		}
    352 	}
    353 
    354 	/* Do just one frame transfer per read */
    355 
    356 	if (!error) {
    357 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
    358 		if (uio->uio_resid < sc->sc_lens[slot]) {
    359 			DPRINTF(("oboe_read: uio buffer smaller than frame size"
    360 			    "(%d < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
    361 			error = EINVAL;
    362 		} else {
    363 			DPRINTF(("oboe_read: moving %d bytes from %p\n",
    364 				 sc->sc_lens[slot],
    365 				 sc->sc_recv_stores[slot]));
    366 			error = uiomove(sc->sc_recv_stores[slot],
    367 					sc->sc_lens[slot], uio);
    368 		}
    369 	}
    370 	sc->sc_saved--;
    371 	splx(s);
    372 
    373 	return (error);
    374 }
    375 
    376 static int
    377 oboe_write(void *h, struct uio *uio, int flag)
    378 {
    379 	struct oboe_softc *sc = h;
    380 	int error = 0;
    381 	int n;
    382 	int s = splir();
    383 
    384 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    385 	while (sc->sc_txpending == TX_SLOTS) {
    386 		if (flag & IO_NDELAY) {
    387 			splx(s);
    388 			return (EWOULDBLOCK);
    389 		}
    390 		sc->sc_state |= OBOE_WSLP;
    391 		DPRINTF(("oboe_write: sleep\n"));
    392 		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
    393 		DPRINTF(("oboe_write: woke up, error=%d\n", error));
    394 		if (error) {
    395 			sc->sc_state &= ~OBOE_WSLP;
    396 			break;
    397 		}
    398 	}
    399 	if (error)
    400 		goto err;
    401 	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
    402 		DPRINTF(("oboe_write: slot overrun\n"));
    403 	}
    404 
    405 	n = irda_sir_frame(sc->sc_xmit_bufs[sc->sc_txs], TX_BUF_SZ, uio,
    406 			   sc->sc_ebofs);
    407 	if (n < 0) {
    408 		error = -n;
    409 		goto err;
    410 	}
    411 	sc->sc_taskfile->xmit[sc->sc_txs].len = n;
    412 
    413 	OUTB(sc, 0, OBOE_RST);
    414 	OUTB(sc, 0x1e, OBOE_REG_11);
    415 
    416 	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
    417 
    418 	/* XXX Need delay here??? */
    419 	delay(1000);
    420 
    421 	sc->sc_txpending++;
    422 	OUTB(sc, 0x80, OBOE_RST);
    423 	OUTB(sc, 1, OBOE_REG_9);
    424 	sc->sc_txs++;
    425 	sc->sc_txs %= TX_SLOTS;
    426 
    427  err:
    428 	splx(s);
    429 	return (error);
    430 }
    431 
    432 static int
    433 oboe_set_params(void *h, struct irda_params *p)
    434 {
    435 	struct oboe_softc *sc = h;
    436 	int error;
    437 
    438 	if (p->speed > 0) {
    439 		error = oboe_setbaud(sc, p->speed);
    440 		if (error)
    441 			return (error);
    442 	}
    443 	sc->sc_ebofs = p->ebofs;
    444 
    445 	/* XXX ignore ebofs and maxsize for now */
    446 	return (0);
    447 }
    448 
    449 static int
    450 oboe_get_speeds(void *h, int *speeds)
    451 {
    452 	struct oboe_softc *sc = h;
    453 	*speeds = sc->sc_speeds;
    454 	return (0);
    455 }
    456 
    457 static int
    458 oboe_get_turnarounds(void *h, int *turnarounds)
    459 {
    460 #ifdef OBOE_DEBUG
    461 	struct oboe_softc *sc = h;
    462 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    463 #endif
    464 
    465 	/* XXX Linux driver sets all bits */
    466 	*turnarounds = IRDA_TURNT_10000; /* 10ms */
    467 
    468 	return (0);
    469 }
    470 
    471 static int
    472 oboe_poll(void *h, int events, struct lwp *l)
    473 {
    474 	struct oboe_softc *sc = h;
    475 	int revents = 0;
    476 	int s;
    477 
    478 	DPRINTF(("%s: sc=%p\n", __func__, sc));
    479 
    480 	s = splir();
    481 	if (events & (POLLOUT | POLLWRNORM))
    482 		revents |= events & (POLLOUT | POLLWRNORM);
    483 	if (events & (POLLIN | POLLRDNORM)) {
    484 		if (sc->sc_saved > 0) {
    485 			DPRINTF(("%s: have data\n", __func__));
    486 			revents |= events & (POLLIN | POLLRDNORM);
    487 		} else {
    488 			DPRINTF(("%s: recording select\n", __func__));
    489 			selrecord(l, &sc->sc_rsel);
    490 		}
    491 	}
    492 	splx(s);
    493 
    494 	return (revents);
    495 }
    496 
    497 static void
    498 filt_oboerdetach(struct knote *kn)
    499 {
    500 	struct oboe_softc *sc = kn->kn_hook;
    501 	int s;
    502 
    503 	s = splir();
    504 	SLIST_REMOVE(&sc->sc_rsel.sel_klist, kn, knote, kn_selnext);
    505 	splx(s);
    506 }
    507 
    508 static int
    509 filt_oboeread(struct knote *kn, long hint)
    510 {
    511 	struct oboe_softc *sc = kn->kn_hook;
    512 
    513 	kn->kn_data = sc->sc_saved;
    514 	return (kn->kn_data > 0);
    515 }
    516 
    517 static void
    518 filt_oboewdetach(struct knote *kn)
    519 {
    520 	struct oboe_softc *sc = kn->kn_hook;
    521 	int s;
    522 
    523 	s = splir();
    524 	SLIST_REMOVE(&sc->sc_wsel.sel_klist, kn, knote, kn_selnext);
    525 	splx(s);
    526 }
    527 
    528 static const struct filterops oboeread_filtops =
    529 	{ 1, NULL, filt_oboerdetach, filt_oboeread };
    530 static const struct filterops oboewrite_filtops =
    531 	{ 1, NULL, filt_oboewdetach, filt_seltrue };
    532 
    533 static int
    534 oboe_kqfilter(void *h, struct knote *kn)
    535 {
    536 	struct oboe_softc *sc = h;
    537 	struct klist *klist;
    538 	int s;
    539 
    540 	switch (kn->kn_filter) {
    541 	case EVFILT_READ:
    542 		klist = &sc->sc_rsel.sel_klist;
    543 		kn->kn_fop = &oboeread_filtops;
    544 		break;
    545 	case EVFILT_WRITE:
    546 		klist = &sc->sc_wsel.sel_klist;
    547 		kn->kn_fop = &oboewrite_filtops;
    548 		break;
    549 	default:
    550 		return (EINVAL);
    551 	}
    552 
    553 	kn->kn_hook = sc;
    554 
    555 	s = splir();
    556 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    557 	splx(s);
    558 
    559 	return (0);
    560 }
    561 
    562 static int
    563 oboe_reset(struct oboe_softc *sc)
    564 {
    565 #if 0
    566 	OUTB(sc, 0x00, OBOE_RST);
    567 	OUTB(sc, 0x80, OBOE_RST);
    568 #endif
    569 	return 0;
    570 }
    571 
    572 static int
    573 oboe_intr(void *p)
    574 {
    575 	struct oboe_softc *sc = p;
    576 	uint8_t irqstat	= INB(sc, OBOE_ISR);
    577 
    578 	if (!(irqstat & 0xf8))
    579 		return (0); /* Not for me? */
    580 
    581 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
    582 
    583 	OUTB(sc, irqstat, OBOE_ISR);
    584 
    585 	if (irqstat & OBOE_ISR_RXDONE) {
    586 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
    587 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
    588 			if (sc->sc_saved == RX_SLOTS) {
    589 				DPRINTF(("oboe_intr: all buffers filled\n"));
    590 				return 0;
    591 			}
    592 
    593 			if (len > 2)
    594 				len -= 2; /* JSP: skip check sum? */
    595 
    596 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
    597 				 sc->sc_recv_stores[sc->sc_rxs]));
    598 			memcpy(sc->sc_recv_stores[sc->sc_rxs],
    599 			       sc->sc_recv_bufs[sc->sc_rxs],
    600 			       len);
    601 			sc->sc_lens[sc->sc_rxs] = len;
    602 			sc->sc_saved++;
    603 #if 0
    604 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
    605 				     len, &sc->sc_q);
    606 #endif
    607 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
    608 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
    609 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
    610 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
    611 		}
    612 		DPRINTF(("oboe_intr no more frames available\n"));
    613 		if (sc->sc_state & OBOE_RSLP) {
    614 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
    615 			sc->sc_state &= ~OBOE_RSLP;
    616 			DPRINTF(("oboe_intr: waking up reader\n"));
    617 			wakeup(&sc->sc_rxs);
    618 		}
    619 		selnotify(&sc->sc_rsel, 0, 0);
    620 		DPRINTF(("oboe_intr returning\n"));
    621 	}
    622 	if (irqstat & OBOE_ISR_TXDONE) {
    623 	        DPRINTF(("oboe_intr: write done\n"));
    624 		sc->sc_txpending--;
    625 		sc->sc_txpackets++;
    626 
    627 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
    628 			wakeup(&sc->sc_state);
    629 			return 1;
    630 		}
    631 
    632 		if (sc->sc_state & OBOE_WSLP) {
    633 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
    634 			sc->sc_state &= ~OBOE_WSLP;
    635 			DPRINTF(("oboe_intr: waking up writer\n"));
    636 			wakeup(&sc->sc_txs);
    637 		}
    638 		selnotify(&sc->sc_wsel, 0, 0);
    639 	}
    640 	return (1);
    641 }
    642 
    643 /* XXX vtophys must go! */
    644 static void
    645 oboe_init_taskfile(struct oboe_softc *sc)
    646 {
    647 	int i;
    648 	int s = splir();
    649 
    650 	for (i = 0; i < TX_SLOTS; ++i) {
    651 		sc->sc_taskfile->xmit[i].len = 0;
    652 		sc->sc_taskfile->xmit[i].control = 0x00;
    653 		sc->sc_taskfile->xmit[i].buffer =
    654 			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
    655 	}
    656 
    657 	for (i = 0; i < RX_SLOTS; ++i) {
    658 		sc->sc_taskfile->recv[i].len = 0;
    659 		sc->sc_taskfile->recv[i].control = 0x83;
    660 		sc->sc_taskfile->recv[i].buffer =
    661 			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
    662 	}
    663 
    664 	sc->sc_txpending = 0;
    665 
    666 	splx(s);
    667 }
    668 
    669 static int
    670 oboe_alloc_taskfile(struct oboe_softc *sc)
    671 {
    672 	int i;
    673 	/* XXX */
    674 	uint32_t addr = (uint32_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
    675 	if (addr == 0) {
    676 		goto bad;
    677 	}
    678 	addr &= ~(sizeof (struct OboeTaskFile) - 1);
    679 	addr += sizeof (struct OboeTaskFile);
    680 	sc->sc_taskfile = (struct OboeTaskFile *) addr;
    681 
    682 	for (i = 0; i < TX_SLOTS; ++i) {
    683 		sc->sc_xmit_bufs[i] =
    684 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    685 		sc->sc_xmit_stores[i] =
    686 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    687 		if (sc->sc_xmit_bufs[i] == NULL ||
    688 		    sc->sc_xmit_stores[i] == NULL) {
    689 			goto bad;
    690 		}
    691 	}
    692 	for (i = 0; i < RX_SLOTS; ++i) {
    693 		sc->sc_recv_bufs[i] =
    694 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    695 		sc->sc_recv_stores[i] =
    696 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    697 		if (sc->sc_recv_bufs[i] == NULL ||
    698 		    sc->sc_recv_stores[i] == NULL) {
    699 			goto bad;
    700 		}
    701 	}
    702 
    703 	return 0;
    704 bad:
    705 	printf("oboe: malloc for buffers failed()\n");
    706 	return 1;
    707 }
    708 
    709 static void
    710 oboe_startchip (struct oboe_softc *sc)
    711 {
    712 	uint32_t physaddr;
    713 
    714 	OUTB(sc, 0, OBOE_LOCK);
    715 	OUTB(sc, 0, OBOE_RST);
    716 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
    717 	OUTB(sc, 0xf0, OBOE_REG_D);
    718 	OUTB(sc, 0xff, OBOE_ISR);
    719 	OUTB(sc, 0x0f, OBOE_REG_1A);
    720 	OUTB(sc, 0xff, OBOE_REG_1B);
    721 
    722 	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
    723 
    724 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
    725 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
    726 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
    727 
    728 	OUTB(sc, 0x0e, OBOE_REG_11);
    729 	OUTB(sc, 0x80, OBOE_RST);
    730 
    731 	(void)oboe_setbaud(sc, 9600);
    732 
    733 	sc->sc_rxs = INB(sc, OBOE_RCVT);
    734 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
    735 		sc->sc_rxs = 0;
    736 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
    737 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
    738 		sc->sc_txs = 0;
    739 }
    740 
    741 static void
    742 oboe_stopchip (struct oboe_softc *sc)
    743 {
    744 	OUTB(sc, 0x0e, OBOE_REG_11);
    745 	OUTB(sc, 0x00, OBOE_RST);
    746 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
    747 	OUTB(sc, 0xff, OBOE_TFP1);
    748 	OUTB(sc, 0xff, OBOE_TFP0);
    749 	OUTB(sc, 0x0f, OBOE_REG_1B);
    750 	OUTB(sc, 0xff, OBOE_REG_1A);
    751 	OUTB(sc, 0x00, OBOE_ISR); /* XXX: should i do this to disable ints? */
    752 	OUTB(sc, 0x80, OBOE_RST);
    753 	OUTB(sc, 0x0e, OBOE_LOCK);
    754 }
    755 
    756 #define SPEEDCASE(speed, type, divisor) \
    757 case speed: \
    758 OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
    759 OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
    760 OUTB(sc, divisor, OBOE_UDIV); \
    761 break
    762 
    763 static int
    764 oboe_setbaud(struct oboe_softc *sc, int baud)
    765 {
    766 	int s;
    767 
    768 	DPRINTF(("oboe: setting baud to %d\n", baud));
    769 
    770 	s = splir();
    771 
    772 	switch (baud) {
    773 	SPEEDCASE(   2400, SIR, 0xbf);
    774 	SPEEDCASE(   9600, SIR, 0x2f);
    775 	SPEEDCASE(  19200, SIR, 0x17);
    776 	SPEEDCASE(  38400, SIR, 0x0b);
    777 	SPEEDCASE(  57600, SIR, 0x07);
    778 	SPEEDCASE( 115200, SIR, 0x03);
    779 	SPEEDCASE(1152000, MIR, 0x01);
    780 	SPEEDCASE(4000000, FIR, 0x00);
    781 	default:
    782 		DPRINTF(("oboe: cannot set speed to %d\n", baud));
    783 		splx(s);
    784 		return (EINVAL);
    785 	}
    786 
    787 	OUTB(sc, 0x00, OBOE_RST);
    788 	OUTB(sc, 0x80, OBOE_RST);
    789 	OUTB(sc, 0x01, OBOE_REG_9);
    790 
    791 	sc->sc_speed = baud;
    792 
    793 	splx(s);
    794 
    795 	return (0);
    796 }
    797