Home | History | Annotate | Line # | Download | only in pci
oboe.c revision 1.5.2.3
      1  1.5.2.3  nathanw /*	$NetBSD: oboe.c,v 1.5.2.3 2002/10/18 02:43:11 nathanw Exp $	*/
      2  1.5.2.2  nathanw 
      3  1.5.2.2  nathanw /*-
      4  1.5.2.2  nathanw  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.5.2.2  nathanw  * All rights reserved.
      6  1.5.2.2  nathanw  *
      7  1.5.2.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5.2.2  nathanw  * by Jan Sparud.
      9  1.5.2.2  nathanw  *
     10  1.5.2.2  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.5.2.2  nathanw  * modification, are permitted provided that the following conditions
     12  1.5.2.2  nathanw  * are met:
     13  1.5.2.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.5.2.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.5.2.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.5.2.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.5.2.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.5.2.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.5.2.2  nathanw  *    must display the following acknowledgement:
     20  1.5.2.2  nathanw  *	This product includes software developed by the NetBSD
     21  1.5.2.2  nathanw  *	Foundation, Inc. and its contributors.
     22  1.5.2.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.5.2.2  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.5.2.2  nathanw  *    from this software without specific prior written permission.
     25  1.5.2.2  nathanw  *
     26  1.5.2.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.5.2.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.5.2.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.5.2.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.5.2.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.5.2.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.5.2.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.5.2.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.5.2.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.5.2.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.5.2.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.5.2.2  nathanw  */
     38  1.5.2.2  nathanw 
     39  1.5.2.2  nathanw /*
     40  1.5.2.2  nathanw  * Toshiba OBOE IrDA SIR/FIR driver.
     41  1.5.2.2  nathanw  *
     42  1.5.2.2  nathanw  * Based on information from the Linux driver, thus the magic hex numbers.
     43  1.5.2.2  nathanw  */
     44  1.5.2.2  nathanw 
     45  1.5.2.2  nathanw #include <sys/param.h>
     46  1.5.2.2  nathanw #include <sys/systm.h>
     47  1.5.2.2  nathanw #include <sys/kernel.h>
     48  1.5.2.2  nathanw #include <sys/device.h>
     49  1.5.2.2  nathanw #include <sys/malloc.h>
     50  1.5.2.2  nathanw #include <sys/tty.h>
     51  1.5.2.2  nathanw #include <sys/vnode.h>
     52  1.5.2.2  nathanw #include <sys/poll.h>
     53  1.5.2.2  nathanw 
     54  1.5.2.2  nathanw #include <dev/ir/ir.h>
     55  1.5.2.2  nathanw #include <dev/ir/irdaio.h>
     56  1.5.2.2  nathanw #include <dev/ir/irframevar.h>
     57  1.5.2.2  nathanw #include <dev/ir/sir.h>
     58  1.5.2.2  nathanw 
     59  1.5.2.2  nathanw #include <dev/pci/pcidevs.h>
     60  1.5.2.2  nathanw #include <dev/pci/pcivar.h>
     61  1.5.2.2  nathanw 
     62  1.5.2.2  nathanw #include <machine/bus.h>
     63  1.5.2.2  nathanw #include <machine/intr.h>
     64  1.5.2.2  nathanw #include <uvm/uvm_extern.h>
     65  1.5.2.2  nathanw 
     66  1.5.2.2  nathanw #include <dev/pci/oboereg.h>
     67  1.5.2.2  nathanw 
     68  1.5.2.2  nathanw int oboe_match(struct device *parent, struct cfdata *match, void *aux);
     69  1.5.2.2  nathanw void oboe_attach(struct device *parent, struct device *self, void *aux);
     70  1.5.2.2  nathanw int oboe_activate(struct device *self, enum devact act);
     71  1.5.2.2  nathanw int oboe_detach(struct device *self, int flags);
     72  1.5.2.2  nathanw 
     73  1.5.2.2  nathanw int oboe_open(void *h, int flag, int mode, struct proc *p);
     74  1.5.2.2  nathanw int oboe_close(void *h, int flag, int mode, struct proc *p);
     75  1.5.2.2  nathanw int oboe_read(void *h, struct uio *uio, int flag);
     76  1.5.2.2  nathanw int oboe_write(void *h, struct uio *uio, int flag);
     77  1.5.2.2  nathanw int oboe_set_params(void *h, struct irda_params *params);
     78  1.5.2.2  nathanw int oboe_get_speeds(void *h, int *speeds);
     79  1.5.2.2  nathanw int oboe_get_turnarounds(void *h, int *times);
     80  1.5.2.2  nathanw int oboe_poll(void *h, int events, struct proc *p);
     81  1.5.2.2  nathanw 
     82  1.5.2.2  nathanw #ifdef OBOE_DEBUG
     83  1.5.2.2  nathanw #define DPRINTF(x)	if (oboedebug) printf x
     84  1.5.2.2  nathanw int oboedebug = 1;
     85  1.5.2.2  nathanw #else
     86  1.5.2.2  nathanw #define DPRINTF(x)
     87  1.5.2.2  nathanw #endif
     88  1.5.2.2  nathanw 
     89  1.5.2.2  nathanw struct oboe_dma;
     90  1.5.2.2  nathanw 
     91  1.5.2.2  nathanw struct oboe_softc {
     92  1.5.2.2  nathanw 	struct device		sc_dev;
     93  1.5.2.2  nathanw 	struct device		*sc_child;
     94  1.5.2.2  nathanw 	struct pci_attach_args	sc_pa;
     95  1.5.2.2  nathanw 	pci_intr_handle_t *	sc_ih;
     96  1.5.2.2  nathanw 	unsigned int		sc_revision;	/* PCI Revision ID */
     97  1.5.2.2  nathanw 	/* I/O Base device */
     98  1.5.2.2  nathanw 	bus_space_tag_t		sc_iot;
     99  1.5.2.2  nathanw 	bus_space_handle_t	sc_ioh;
    100  1.5.2.2  nathanw 	bus_dma_tag_t		sc_dmatag;
    101  1.5.2.2  nathanw 	struct selinfo		sc_rsel;
    102  1.5.2.2  nathanw 
    103  1.5.2.2  nathanw 	int			sc_state;
    104  1.5.2.2  nathanw #define	OBOE_RSLP		0x01	/* waiting for data (read) */
    105  1.5.2.2  nathanw #define	OBOE_WSLP		0x02	/* waiting for data (write) */
    106  1.5.2.2  nathanw #define OBOE_CLOSING		0x04	/* waiting for output to drain */
    107  1.5.2.2  nathanw 
    108  1.5.2.2  nathanw 	int			sc_speeds;
    109  1.5.2.2  nathanw 	int			sc_flags;
    110  1.5.2.2  nathanw 	int			sc_speed;
    111  1.5.2.2  nathanw 	int			sc_ebofs;
    112  1.5.2.2  nathanw 
    113  1.5.2.2  nathanw 	struct oboe_dma		*sc_dmas;
    114  1.5.2.2  nathanw 	struct OboeTaskFile	*sc_taskfile;    /* The taskfile   */
    115  1.5.2.2  nathanw 	u_char *		sc_xmit_bufs[TX_SLOTS];
    116  1.5.2.2  nathanw 	u_char *		sc_recv_bufs[RX_SLOTS];
    117  1.5.2.2  nathanw 	void *			sc_xmit_stores[TX_SLOTS];
    118  1.5.2.2  nathanw 	void *			sc_recv_stores[RX_SLOTS];
    119  1.5.2.2  nathanw 	int			sc_txs; /* Current transmit slot number */
    120  1.5.2.2  nathanw 	int			sc_rxs; /* Current receive slot number */
    121  1.5.2.2  nathanw 	int			sc_saved; /* number of saved frames */
    122  1.5.2.2  nathanw 	int			sc_lens[RX_SLOTS];
    123  1.5.2.2  nathanw 
    124  1.5.2.2  nathanw 	int			sc_txpending;
    125  1.5.2.2  nathanw 
    126  1.5.2.2  nathanw 	/* Statistics */
    127  1.5.2.2  nathanw 	int			sc_txpackets;
    128  1.5.2.2  nathanw 	int			sc_rxpackets;
    129  1.5.2.2  nathanw 	int			sc_txerrors;
    130  1.5.2.2  nathanw 	int			sc_rxerrors;
    131  1.5.2.2  nathanw };
    132  1.5.2.2  nathanw 
    133  1.5.2.2  nathanw static int oboe_intr(void *handle);
    134  1.5.2.2  nathanw static int oboe_reset(struct oboe_softc *);
    135  1.5.2.2  nathanw 
    136  1.5.2.2  nathanw struct oboe_dma {
    137  1.5.2.2  nathanw 	bus_dmamap_t map;
    138  1.5.2.2  nathanw 	caddr_t addr;
    139  1.5.2.2  nathanw 	bus_dma_segment_t segs[1];
    140  1.5.2.2  nathanw 	int nsegs;
    141  1.5.2.2  nathanw 	size_t size;
    142  1.5.2.2  nathanw 	struct oboe_dma *next;
    143  1.5.2.2  nathanw };
    144  1.5.2.2  nathanw 
    145  1.5.2.2  nathanw #define DMAADDR(p) ((p)->map->dm_segs[0].ds_addr)
    146  1.5.2.2  nathanw #define KERNADDR(p) ((void *)((p)->addr))
    147  1.5.2.2  nathanw 
    148  1.5.2.2  nathanw static int oboe_alloc_taskfile(struct oboe_softc *);
    149  1.5.2.2  nathanw static void oboe_init_taskfile(struct oboe_softc *);
    150  1.5.2.2  nathanw static void oboe_startchip(struct oboe_softc *);
    151  1.5.2.2  nathanw static void oboe_stopchip(struct oboe_softc *);
    152  1.5.2.2  nathanw static int oboe_setbaud(struct oboe_softc *, int);
    153  1.5.2.2  nathanw 
    154  1.5.2.3  nathanw CFATTACH_DECL(oboe, sizeof(struct oboe_softc),
    155  1.5.2.3  nathanw     oboe_match, oboe_attach, oboe_detach, oboe_activate);
    156  1.5.2.2  nathanw 
    157  1.5.2.2  nathanw struct irframe_methods oboe_methods = {
    158  1.5.2.2  nathanw 	oboe_open, oboe_close, oboe_read, oboe_write, oboe_poll,
    159  1.5.2.2  nathanw 	oboe_set_params, oboe_get_speeds, oboe_get_turnarounds
    160  1.5.2.2  nathanw };
    161  1.5.2.2  nathanw 
    162  1.5.2.2  nathanw int
    163  1.5.2.2  nathanw oboe_match(struct device *parent, struct cfdata *match, void *aux)
    164  1.5.2.2  nathanw {
    165  1.5.2.2  nathanw 	struct pci_attach_args *pa = aux;
    166  1.5.2.2  nathanw 
    167  1.5.2.2  nathanw 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TOSHIBA2 &&
    168  1.5.2.2  nathanw 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_OBOE ||
    169  1.5.2.2  nathanw 	     PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TOSHIBA2_DONAUOBOE))
    170  1.5.2.2  nathanw 		return (1);
    171  1.5.2.2  nathanw 	return 0;
    172  1.5.2.2  nathanw }
    173  1.5.2.2  nathanw 
    174  1.5.2.2  nathanw void
    175  1.5.2.2  nathanw oboe_attach(struct device *parent, struct device *self, void *aux)
    176  1.5.2.2  nathanw {
    177  1.5.2.2  nathanw 	struct oboe_softc *sc = (struct oboe_softc *)self;
    178  1.5.2.2  nathanw 	struct pci_attach_args *pa = aux;
    179  1.5.2.2  nathanw 	pci_intr_handle_t ih;
    180  1.5.2.2  nathanw 	struct ir_attach_args ia;
    181  1.5.2.2  nathanw 	const char *intrstring;
    182  1.5.2.2  nathanw 
    183  1.5.2.2  nathanw 	sc->sc_revision = PCI_REVISION(pa->pa_class);
    184  1.5.2.2  nathanw 	printf(": Toshiba Fast Infrared Type O, revision %d\n",
    185  1.5.2.2  nathanw 	       sc->sc_revision);
    186  1.5.2.2  nathanw 
    187  1.5.2.2  nathanw 	/* Map I/O registers. */
    188  1.5.2.2  nathanw 	if (pci_mapreg_map(pa, IO_BAR, PCI_MAPREG_TYPE_IO, 0,
    189  1.5.2.2  nathanw 	    &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
    190  1.5.2.2  nathanw 		printf("%s: can't map I/O space\n", sc->sc_dev.dv_xname);
    191  1.5.2.2  nathanw 		return;
    192  1.5.2.2  nathanw 	}
    193  1.5.2.2  nathanw 
    194  1.5.2.2  nathanw 	sc->sc_dmatag = pa->pa_dmat;
    195  1.5.2.2  nathanw 
    196  1.5.2.2  nathanw 	ia.ia_type = IR_TYPE_IRFRAME;
    197  1.5.2.2  nathanw 	ia.ia_methods = &oboe_methods;
    198  1.5.2.2  nathanw 	ia.ia_handle = sc;
    199  1.5.2.2  nathanw 
    200  1.5.2.2  nathanw 	sc->sc_state = 0;
    201  1.5.2.2  nathanw 	sc->sc_speed = IRDA_SPEED_9600;
    202  1.5.2.2  nathanw 
    203  1.5.2.2  nathanw 	/* Enable the device */
    204  1.5.2.2  nathanw 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    205  1.5.2.2  nathanw 	    pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
    206  1.5.2.2  nathanw 	    PCI_COMMAND_MASTER_ENABLE);
    207  1.5.2.2  nathanw 
    208  1.5.2.2  nathanw 	/* Reset the device; bail out upon failure. */
    209  1.5.2.2  nathanw 	if (oboe_reset(sc) != 0) {
    210  1.5.2.2  nathanw 		printf("%s: can't reset\n", sc->sc_dev.dv_xname);
    211  1.5.2.2  nathanw 		return;
    212  1.5.2.2  nathanw 	}
    213  1.5.2.2  nathanw 	/* Map and establish the interrupt. */
    214  1.5.2.2  nathanw 	if (pci_intr_map(pa, &ih)) {
    215  1.5.2.2  nathanw 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
    216  1.5.2.2  nathanw 		return;
    217  1.5.2.2  nathanw 	}
    218  1.5.2.2  nathanw 	intrstring = pci_intr_string(pa->pa_pc, ih);
    219  1.5.2.2  nathanw 	sc->sc_ih  = pci_intr_establish(pa->pa_pc, ih, IPL_IR, oboe_intr, sc);
    220  1.5.2.2  nathanw 	if (sc->sc_ih == NULL) {
    221  1.5.2.2  nathanw 		printf("%s: couldn't establish interrupt",
    222  1.5.2.2  nathanw 		    sc->sc_dev.dv_xname);
    223  1.5.2.2  nathanw 		if (intrstring != NULL)
    224  1.5.2.2  nathanw 			printf(" at %s", intrstring);
    225  1.5.2.2  nathanw 		printf("\n");
    226  1.5.2.2  nathanw 		return;
    227  1.5.2.2  nathanw 	}
    228  1.5.2.2  nathanw 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstring);
    229  1.5.2.2  nathanw 
    230  1.5.2.2  nathanw 	sc->sc_txs = 0;
    231  1.5.2.2  nathanw 	sc->sc_rxs = 0;
    232  1.5.2.2  nathanw 
    233  1.5.2.2  nathanw 	sc->sc_speeds =
    234  1.5.2.2  nathanw 		IRDA_SPEED_2400   | IRDA_SPEED_9600    | IRDA_SPEED_19200 |
    235  1.5.2.2  nathanw 		IRDA_SPEED_38400  | IRDA_SPEED_57600   | IRDA_SPEED_115200 |
    236  1.5.2.2  nathanw 		IRDA_SPEED_576000 | IRDA_SPEED_1152000 | IRDA_SPEED_4000000;
    237  1.5.2.2  nathanw 
    238  1.5.2.2  nathanw 	oboe_alloc_taskfile(sc);
    239  1.5.2.2  nathanw 
    240  1.5.2.2  nathanw 	sc->sc_child = config_found((void *)sc, &ia, ir_print);
    241  1.5.2.2  nathanw }
    242  1.5.2.2  nathanw 
    243  1.5.2.2  nathanw int
    244  1.5.2.2  nathanw oboe_activate(struct device *self, enum devact act)
    245  1.5.2.2  nathanw {
    246  1.5.2.2  nathanw 	struct oboe_softc *sc = (struct oboe_softc *)self;
    247  1.5.2.2  nathanw 	int error;
    248  1.5.2.2  nathanw 
    249  1.5.2.2  nathanw 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    250  1.5.2.2  nathanw 
    251  1.5.2.2  nathanw 	switch (act) {
    252  1.5.2.2  nathanw 	case DVACT_ACTIVATE:
    253  1.5.2.2  nathanw 		return (EOPNOTSUPP);
    254  1.5.2.2  nathanw 		break;
    255  1.5.2.2  nathanw 
    256  1.5.2.2  nathanw 	case DVACT_DEACTIVATE:
    257  1.5.2.2  nathanw 		if (sc->sc_child != NULL)
    258  1.5.2.2  nathanw 			error = config_deactivate(sc->sc_child);
    259  1.5.2.2  nathanw 		else
    260  1.5.2.2  nathanw 			error = 0;
    261  1.5.2.2  nathanw 		break;
    262  1.5.2.2  nathanw 	}
    263  1.5.2.2  nathanw 	return (error);
    264  1.5.2.2  nathanw }
    265  1.5.2.2  nathanw 
    266  1.5.2.2  nathanw int
    267  1.5.2.2  nathanw oboe_detach(struct device *self, int flags)
    268  1.5.2.2  nathanw {
    269  1.5.2.2  nathanw #if 0
    270  1.5.2.2  nathanw 	struct oboe_softc *sc = (struct oboe_softc *)self;
    271  1.5.2.2  nathanw #endif
    272  1.5.2.2  nathanw 	/* XXX needs reference counting for proper detach. */
    273  1.5.2.2  nathanw 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    274  1.5.2.2  nathanw 	return (0);
    275  1.5.2.2  nathanw }
    276  1.5.2.2  nathanw 
    277  1.5.2.2  nathanw int
    278  1.5.2.2  nathanw oboe_open(void *h, int flag, int mode, struct proc *p)
    279  1.5.2.2  nathanw {
    280  1.5.2.2  nathanw 	struct oboe_softc *sc = h;
    281  1.5.2.2  nathanw 
    282  1.5.2.2  nathanw 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    283  1.5.2.2  nathanw 
    284  1.5.2.2  nathanw 	sc->sc_state = 0;
    285  1.5.2.2  nathanw 	sc->sc_saved = 0;
    286  1.5.2.2  nathanw 	oboe_init_taskfile(sc);
    287  1.5.2.2  nathanw 	oboe_startchip(sc);
    288  1.5.2.2  nathanw 
    289  1.5.2.2  nathanw 	return (0);
    290  1.5.2.2  nathanw }
    291  1.5.2.2  nathanw 
    292  1.5.2.2  nathanw int
    293  1.5.2.2  nathanw oboe_close(void *h, int flag, int mode, struct proc *p)
    294  1.5.2.2  nathanw {
    295  1.5.2.2  nathanw 	struct oboe_softc *sc = h;
    296  1.5.2.2  nathanw 	int error = 0;
    297  1.5.2.2  nathanw 	int s = splir();
    298  1.5.2.2  nathanw 
    299  1.5.2.2  nathanw 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    300  1.5.2.2  nathanw 	/* Wait for output to drain */
    301  1.5.2.2  nathanw 
    302  1.5.2.2  nathanw 	if (sc->sc_txpending > 0) {
    303  1.5.2.2  nathanw 		sc->sc_state |= OBOE_CLOSING;
    304  1.5.2.2  nathanw 		error = tsleep(&sc->sc_state, PZERO | PCATCH, "oboecl", hz/10);
    305  1.5.2.2  nathanw 	}
    306  1.5.2.2  nathanw 	splx(s);
    307  1.5.2.2  nathanw 
    308  1.5.2.2  nathanw 	oboe_stopchip(sc);
    309  1.5.2.2  nathanw 	return (error);
    310  1.5.2.2  nathanw }
    311  1.5.2.2  nathanw 
    312  1.5.2.2  nathanw int
    313  1.5.2.2  nathanw oboe_read(void *h, struct uio *uio, int flag)
    314  1.5.2.2  nathanw {
    315  1.5.2.2  nathanw 	struct oboe_softc *sc = h;
    316  1.5.2.2  nathanw 	int error = 0;
    317  1.5.2.2  nathanw 	int s;
    318  1.5.2.2  nathanw 	int slot;
    319  1.5.2.2  nathanw 
    320  1.5.2.2  nathanw 	DPRINTF(("%s: resid=%d, iovcnt=%d, offset=%ld\n",
    321  1.5.2.2  nathanw 		 __FUNCTION__, uio->uio_resid, uio->uio_iovcnt,
    322  1.5.2.2  nathanw 		 (long)uio->uio_offset));
    323  1.5.2.2  nathanw 
    324  1.5.2.2  nathanw 	s = splir();
    325  1.5.2.2  nathanw 	while (sc->sc_saved == 0) {
    326  1.5.2.2  nathanw 		if (flag & IO_NDELAY) {
    327  1.5.2.2  nathanw 			splx(s);
    328  1.5.2.2  nathanw 			return (EWOULDBLOCK);
    329  1.5.2.2  nathanw 		}
    330  1.5.2.2  nathanw 		sc->sc_state |= OBOE_RSLP;
    331  1.5.2.2  nathanw 		DPRINTF(("oboe_read: sleep\n"));
    332  1.5.2.2  nathanw 		error = tsleep(&sc->sc_rxs, PZERO | PCATCH, "oboerd", 0);
    333  1.5.2.2  nathanw 		DPRINTF(("oboe_read: woke, error=%d\n", error));
    334  1.5.2.2  nathanw 		if (error) {
    335  1.5.2.2  nathanw 			sc->sc_state &= ~OBOE_RSLP;
    336  1.5.2.2  nathanw 			break;
    337  1.5.2.2  nathanw 		}
    338  1.5.2.2  nathanw 	}
    339  1.5.2.2  nathanw 
    340  1.5.2.2  nathanw 	/* Do just one frame transfer per read */
    341  1.5.2.2  nathanw 
    342  1.5.2.2  nathanw 	if (!error) {
    343  1.5.2.2  nathanw 		slot = (sc->sc_rxs - sc->sc_saved + RX_SLOTS) % RX_SLOTS;
    344  1.5.2.2  nathanw 		if (uio->uio_resid < sc->sc_lens[slot]) {
    345  1.5.2.2  nathanw 			DPRINTF(("oboe_read: uio buffer smaller than frame size"
    346  1.5.2.2  nathanw 			    "(%d < %d)\n", uio->uio_resid, sc->sc_lens[slot]));
    347  1.5.2.2  nathanw 			error = EINVAL;
    348  1.5.2.2  nathanw 		} else {
    349  1.5.2.2  nathanw 			DPRINTF(("oboe_read: moving %d bytes from %p\n",
    350  1.5.2.2  nathanw 				 sc->sc_lens[slot],
    351  1.5.2.2  nathanw 				 sc->sc_recv_stores[slot]));
    352  1.5.2.2  nathanw 			error = uiomove(sc->sc_recv_stores[slot],
    353  1.5.2.2  nathanw 					sc->sc_lens[slot], uio);
    354  1.5.2.2  nathanw 		}
    355  1.5.2.2  nathanw 	}
    356  1.5.2.2  nathanw 	sc->sc_saved--;
    357  1.5.2.2  nathanw 	splx(s);
    358  1.5.2.2  nathanw 
    359  1.5.2.2  nathanw 	return (error);
    360  1.5.2.2  nathanw }
    361  1.5.2.2  nathanw 
    362  1.5.2.2  nathanw int
    363  1.5.2.2  nathanw oboe_write(void *h, struct uio *uio, int flag)
    364  1.5.2.2  nathanw {
    365  1.5.2.2  nathanw 	struct oboe_softc *sc = h;
    366  1.5.2.2  nathanw 	int error = 0;
    367  1.5.2.2  nathanw 	int n;
    368  1.5.2.2  nathanw 	int s = splir();
    369  1.5.2.2  nathanw 
    370  1.5.2.2  nathanw 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    371  1.5.2.2  nathanw 	while (sc->sc_txpending == TX_SLOTS) {
    372  1.5.2.2  nathanw 		if (flag & IO_NDELAY) {
    373  1.5.2.2  nathanw 			splx(s);
    374  1.5.2.2  nathanw 			return (EWOULDBLOCK);
    375  1.5.2.2  nathanw 		}
    376  1.5.2.2  nathanw 		sc->sc_state |= OBOE_WSLP;
    377  1.5.2.2  nathanw 		DPRINTF(("oboe_write: sleep\n"));
    378  1.5.2.2  nathanw 		error = tsleep(&sc->sc_txs, PZERO | PCATCH, "oboewr", 0);
    379  1.5.2.2  nathanw 		DPRINTF(("oboe_write: woke up, error=%d\n", error));
    380  1.5.2.2  nathanw 		if (error) {
    381  1.5.2.2  nathanw 			sc->sc_state &= ~OBOE_WSLP;
    382  1.5.2.2  nathanw 			break;
    383  1.5.2.2  nathanw 		}
    384  1.5.2.2  nathanw 	}
    385  1.5.2.2  nathanw 	if (error)
    386  1.5.2.2  nathanw 		goto err;
    387  1.5.2.2  nathanw 	if (sc->sc_taskfile->xmit[sc->sc_txs].control) {
    388  1.5.2.2  nathanw 		DPRINTF(("oboe_write: slot overrun\n"));
    389  1.5.2.2  nathanw 	}
    390  1.5.2.2  nathanw 
    391  1.5.2.2  nathanw 	n = irda_sir_frame(sc->sc_xmit_bufs[sc->sc_txs], TX_BUF_SZ, uio,
    392  1.5.2.2  nathanw 			   sc->sc_ebofs);
    393  1.5.2.2  nathanw 	if (n < 0) {
    394  1.5.2.2  nathanw 		error = -n;
    395  1.5.2.2  nathanw 		goto err;
    396  1.5.2.2  nathanw 	}
    397  1.5.2.2  nathanw 	sc->sc_taskfile->xmit[sc->sc_txs].len = n;
    398  1.5.2.2  nathanw 
    399  1.5.2.2  nathanw 	OUTB(sc, 0, OBOE_RST);
    400  1.5.2.2  nathanw 	OUTB(sc, 0x1e, OBOE_REG_11);
    401  1.5.2.2  nathanw 
    402  1.5.2.2  nathanw 	sc->sc_taskfile->xmit[sc->sc_txs].control = 0x84;
    403  1.5.2.2  nathanw 
    404  1.5.2.2  nathanw 	/* XXX Need delay here??? */
    405  1.5.2.2  nathanw 	delay(1000);
    406  1.5.2.2  nathanw 
    407  1.5.2.2  nathanw 	sc->sc_txpending++;
    408  1.5.2.2  nathanw 	OUTB(sc, 0x80, OBOE_RST);
    409  1.5.2.2  nathanw 	OUTB(sc, 1, OBOE_REG_9);
    410  1.5.2.2  nathanw 	sc->sc_txs++;
    411  1.5.2.2  nathanw 	sc->sc_txs %= TX_SLOTS;
    412  1.5.2.2  nathanw 
    413  1.5.2.2  nathanw  err:
    414  1.5.2.2  nathanw 	splx(s);
    415  1.5.2.2  nathanw 	return (error);
    416  1.5.2.2  nathanw }
    417  1.5.2.2  nathanw 
    418  1.5.2.2  nathanw int
    419  1.5.2.2  nathanw oboe_set_params(void *h, struct irda_params *p)
    420  1.5.2.2  nathanw {
    421  1.5.2.2  nathanw 	struct oboe_softc *sc = h;
    422  1.5.2.2  nathanw 	int error;
    423  1.5.2.2  nathanw 
    424  1.5.2.2  nathanw 	if (p->speed > 0) {
    425  1.5.2.2  nathanw 		error = oboe_setbaud(sc, p->speed);
    426  1.5.2.2  nathanw 		if (error)
    427  1.5.2.2  nathanw 			return (error);
    428  1.5.2.2  nathanw 	}
    429  1.5.2.2  nathanw 	sc->sc_ebofs = p->ebofs;
    430  1.5.2.2  nathanw 
    431  1.5.2.2  nathanw 	/* XXX ignore ebofs and maxsize for now */
    432  1.5.2.2  nathanw 	return (0);
    433  1.5.2.2  nathanw }
    434  1.5.2.2  nathanw 
    435  1.5.2.2  nathanw int
    436  1.5.2.2  nathanw oboe_get_speeds(void *h, int *speeds)
    437  1.5.2.2  nathanw {
    438  1.5.2.2  nathanw 	struct oboe_softc *sc = h;
    439  1.5.2.2  nathanw 	*speeds = sc->sc_speeds;
    440  1.5.2.2  nathanw 	return (0);
    441  1.5.2.2  nathanw }
    442  1.5.2.2  nathanw 
    443  1.5.2.2  nathanw int
    444  1.5.2.2  nathanw oboe_get_turnarounds(void *h, int *turnarounds)
    445  1.5.2.2  nathanw {
    446  1.5.2.2  nathanw #if 0
    447  1.5.2.2  nathanw 	struct oboe_softc *sc = h;
    448  1.5.2.2  nathanw #endif
    449  1.5.2.2  nathanw 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    450  1.5.2.2  nathanw 	/* XXX Linux driver sets all bits */
    451  1.5.2.2  nathanw 	*turnarounds = IRDA_TURNT_10000; /* 10ms */
    452  1.5.2.2  nathanw 	return (0);
    453  1.5.2.2  nathanw }
    454  1.5.2.2  nathanw 
    455  1.5.2.2  nathanw int
    456  1.5.2.2  nathanw oboe_poll(void *h, int events, struct proc *p)
    457  1.5.2.2  nathanw {
    458  1.5.2.2  nathanw 	struct oboe_softc *sc = h;
    459  1.5.2.2  nathanw 	int revents = 0;
    460  1.5.2.2  nathanw 	int s;
    461  1.5.2.2  nathanw 
    462  1.5.2.2  nathanw 	DPRINTF(("%s: sc=%p\n", __FUNCTION__, sc));
    463  1.5.2.2  nathanw 
    464  1.5.2.2  nathanw 	s = splir();
    465  1.5.2.2  nathanw 	if (events & (POLLOUT | POLLWRNORM))
    466  1.5.2.2  nathanw 		revents |= events & (POLLOUT | POLLWRNORM);
    467  1.5.2.2  nathanw 	if (events & (POLLIN | POLLRDNORM)) {
    468  1.5.2.2  nathanw 		if (sc->sc_saved > 0) {
    469  1.5.2.2  nathanw 			DPRINTF(("%s: have data\n", __FUNCTION__));
    470  1.5.2.2  nathanw 			revents |= events & (POLLIN | POLLRDNORM);
    471  1.5.2.2  nathanw 		} else {
    472  1.5.2.2  nathanw 			DPRINTF(("%s: recording select\n", __FUNCTION__));
    473  1.5.2.2  nathanw 			selrecord(p, &sc->sc_rsel);
    474  1.5.2.2  nathanw 		}
    475  1.5.2.2  nathanw 	}
    476  1.5.2.2  nathanw 	splx(s);
    477  1.5.2.2  nathanw 
    478  1.5.2.2  nathanw 	return (revents);
    479  1.5.2.2  nathanw }
    480  1.5.2.2  nathanw 
    481  1.5.2.2  nathanw 
    482  1.5.2.2  nathanw static int
    483  1.5.2.2  nathanw oboe_reset(struct oboe_softc *sc)
    484  1.5.2.2  nathanw {
    485  1.5.2.2  nathanw #if 0
    486  1.5.2.2  nathanw 	OUTB(sc, 0x00, OBOE_RST);
    487  1.5.2.2  nathanw 	OUTB(sc, 0x80, OBOE_RST);
    488  1.5.2.2  nathanw #endif
    489  1.5.2.2  nathanw 	return 0;
    490  1.5.2.2  nathanw }
    491  1.5.2.2  nathanw 
    492  1.5.2.2  nathanw static int
    493  1.5.2.2  nathanw oboe_intr(void *p)
    494  1.5.2.2  nathanw {
    495  1.5.2.2  nathanw 	struct oboe_softc *sc = p;
    496  1.5.2.2  nathanw 	uint8_t irqstat	= INB(sc, OBOE_ISR);
    497  1.5.2.2  nathanw 
    498  1.5.2.2  nathanw 	if (!(irqstat & 0xf8))
    499  1.5.2.2  nathanw 		return (0); /* Not for me? */
    500  1.5.2.2  nathanw 
    501  1.5.2.2  nathanw 	DPRINTF(("oboe_intr stat=0x%x\n", irqstat));
    502  1.5.2.2  nathanw 
    503  1.5.2.2  nathanw 	OUTB(sc, irqstat, OBOE_ISR);
    504  1.5.2.2  nathanw 
    505  1.5.2.2  nathanw 	if (irqstat & OBOE_ISR_RXDONE) {
    506  1.5.2.2  nathanw 		while (sc->sc_taskfile->recv[sc->sc_rxs].control == 0) {
    507  1.5.2.2  nathanw 			int len = sc->sc_taskfile->recv[sc->sc_rxs].len;
    508  1.5.2.2  nathanw 			if (sc->sc_saved == RX_SLOTS) {
    509  1.5.2.2  nathanw 				DPRINTF(("oboe_intr: all buffers filled\n"));
    510  1.5.2.2  nathanw 				return 0;
    511  1.5.2.2  nathanw 			}
    512  1.5.2.2  nathanw 
    513  1.5.2.2  nathanw 			if (len > 2)
    514  1.5.2.2  nathanw 				len -= 2; /* JSP: skip check sum? */
    515  1.5.2.2  nathanw 
    516  1.5.2.2  nathanw 			DPRINTF(("oboe_intr: moving %d bytes to %p\n", len,
    517  1.5.2.2  nathanw 				 sc->sc_recv_stores[sc->sc_rxs]));
    518  1.5.2.2  nathanw 			memcpy(sc->sc_recv_stores[sc->sc_rxs],
    519  1.5.2.2  nathanw 			       sc->sc_recv_bufs[sc->sc_rxs],
    520  1.5.2.2  nathanw 			       len);
    521  1.5.2.2  nathanw 			sc->sc_lens[sc->sc_rxs] = len;
    522  1.5.2.2  nathanw 			sc->sc_saved++;
    523  1.5.2.2  nathanw #if 0
    524  1.5.2.2  nathanw 			(void)b_to_q(sc->sc_recv_bufs[sc->sc_rxs],
    525  1.5.2.2  nathanw 				     len, &sc->sc_q);
    526  1.5.2.2  nathanw #endif
    527  1.5.2.2  nathanw 			sc->sc_taskfile->recv[sc->sc_rxs].control = 0x83;
    528  1.5.2.2  nathanw 			sc->sc_taskfile->recv[sc->sc_rxs].len = 0x0;
    529  1.5.2.2  nathanw 			sc->sc_rxs = (sc->sc_rxs + 1) % RX_SLOTS;
    530  1.5.2.2  nathanw 			DPRINTF(("oboe_intr new rxs=%d\n", sc->sc_rxs));
    531  1.5.2.2  nathanw 		}
    532  1.5.2.2  nathanw 		DPRINTF(("oboe_intr no more frames available\n"));
    533  1.5.2.2  nathanw 		if (sc->sc_state & OBOE_RSLP) {
    534  1.5.2.2  nathanw 			DPRINTF(("oboe_intr changing state to ~OBOE_RSLP\n"));
    535  1.5.2.2  nathanw 			sc->sc_state &= ~OBOE_RSLP;
    536  1.5.2.2  nathanw 			DPRINTF(("oboe_intr: waking up reader\n"));
    537  1.5.2.2  nathanw 			wakeup(&sc->sc_rxs);
    538  1.5.2.2  nathanw 		}
    539  1.5.2.2  nathanw 		selwakeup(&sc->sc_rsel);
    540  1.5.2.2  nathanw 		DPRINTF(("oboe_intr returning\n"));
    541  1.5.2.2  nathanw 	}
    542  1.5.2.2  nathanw 	if (irqstat & OBOE_ISR_TXDONE) {
    543  1.5.2.2  nathanw 	        DPRINTF(("oboe_intr: write done\n"));
    544  1.5.2.2  nathanw 		sc->sc_txpending--;
    545  1.5.2.2  nathanw 		sc->sc_txpackets++;
    546  1.5.2.2  nathanw 
    547  1.5.2.2  nathanw 		if ((sc->sc_state & OBOE_CLOSING) && sc->sc_txpending == 0) {
    548  1.5.2.2  nathanw 			wakeup(&sc->sc_state);
    549  1.5.2.2  nathanw 			return 1;
    550  1.5.2.2  nathanw 		}
    551  1.5.2.2  nathanw 
    552  1.5.2.2  nathanw 		if (sc->sc_state & OBOE_WSLP) {
    553  1.5.2.2  nathanw 			DPRINTF(("oboe_intr changing state to ~OBOE_WSLP\n"));
    554  1.5.2.2  nathanw 			sc->sc_state &= ~OBOE_WSLP;
    555  1.5.2.2  nathanw 			DPRINTF(("oboe_intr: waking up writer\n"));
    556  1.5.2.2  nathanw 			wakeup(&sc->sc_txs);
    557  1.5.2.2  nathanw 		}
    558  1.5.2.2  nathanw 	}
    559  1.5.2.2  nathanw 	return (1);
    560  1.5.2.2  nathanw }
    561  1.5.2.2  nathanw 
    562  1.5.2.2  nathanw /* XXX vtophys must go! */
    563  1.5.2.2  nathanw static void
    564  1.5.2.2  nathanw oboe_init_taskfile(struct oboe_softc *sc)
    565  1.5.2.2  nathanw {
    566  1.5.2.2  nathanw 	int i;
    567  1.5.2.2  nathanw 	int s = splir();
    568  1.5.2.2  nathanw 
    569  1.5.2.2  nathanw 	for (i = 0; i < TX_SLOTS; ++i) {
    570  1.5.2.2  nathanw 		sc->sc_taskfile->xmit[i].len = 0;
    571  1.5.2.2  nathanw 		sc->sc_taskfile->xmit[i].control = 0x00;
    572  1.5.2.2  nathanw 		sc->sc_taskfile->xmit[i].buffer =
    573  1.5.2.2  nathanw 			vtophys((u_int)sc->sc_xmit_bufs[i]); /* u_int? */
    574  1.5.2.2  nathanw 	}
    575  1.5.2.2  nathanw 
    576  1.5.2.2  nathanw 	for (i = 0; i < RX_SLOTS; ++i) {
    577  1.5.2.2  nathanw 		sc->sc_taskfile->recv[i].len = 0;
    578  1.5.2.2  nathanw 		sc->sc_taskfile->recv[i].control = 0x83;
    579  1.5.2.2  nathanw 		sc->sc_taskfile->recv[i].buffer =
    580  1.5.2.2  nathanw 			vtophys((u_int)sc->sc_recv_bufs[i]); /* u_int? */
    581  1.5.2.2  nathanw 	}
    582  1.5.2.2  nathanw 
    583  1.5.2.2  nathanw 	sc->sc_txpending = 0;
    584  1.5.2.2  nathanw 
    585  1.5.2.2  nathanw 	splx(s);
    586  1.5.2.2  nathanw }
    587  1.5.2.2  nathanw 
    588  1.5.2.2  nathanw static int
    589  1.5.2.2  nathanw oboe_alloc_taskfile(struct oboe_softc *sc)
    590  1.5.2.2  nathanw {
    591  1.5.2.2  nathanw 	int i;
    592  1.5.2.2  nathanw 	/* XXX */
    593  1.5.2.2  nathanw 	uint32_t addr = (uint32_t)malloc(OBOE_TASK_BUF_LEN, M_DEVBUF, M_WAITOK);
    594  1.5.2.2  nathanw 	if (addr == NULL) {
    595  1.5.2.2  nathanw 		goto bad;
    596  1.5.2.2  nathanw 	}
    597  1.5.2.2  nathanw 	addr &= ~(sizeof (struct OboeTaskFile) - 1);
    598  1.5.2.2  nathanw 	addr += sizeof (struct OboeTaskFile);
    599  1.5.2.2  nathanw 	sc->sc_taskfile = (struct OboeTaskFile *) addr;
    600  1.5.2.2  nathanw 
    601  1.5.2.2  nathanw 	for (i = 0; i < TX_SLOTS; ++i) {
    602  1.5.2.2  nathanw 		sc->sc_xmit_bufs[i] =
    603  1.5.2.2  nathanw 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    604  1.5.2.2  nathanw 		sc->sc_xmit_stores[i] =
    605  1.5.2.2  nathanw 			malloc(TX_BUF_SZ, M_DEVBUF, M_WAITOK);
    606  1.5.2.2  nathanw 		if (sc->sc_xmit_bufs[i] == NULL ||
    607  1.5.2.2  nathanw 		    sc->sc_xmit_stores[i] == NULL) {
    608  1.5.2.2  nathanw 			goto bad;
    609  1.5.2.2  nathanw 		}
    610  1.5.2.2  nathanw 	}
    611  1.5.2.2  nathanw 	for (i = 0; i < RX_SLOTS; ++i) {
    612  1.5.2.2  nathanw 		sc->sc_recv_bufs[i] =
    613  1.5.2.2  nathanw 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    614  1.5.2.2  nathanw 		sc->sc_recv_stores[i] =
    615  1.5.2.2  nathanw 			malloc(RX_BUF_SZ, M_DEVBUF, M_WAITOK);
    616  1.5.2.2  nathanw 		if (sc->sc_recv_bufs[i] == NULL ||
    617  1.5.2.2  nathanw 		    sc->sc_recv_stores[i] == NULL) {
    618  1.5.2.2  nathanw 			goto bad;
    619  1.5.2.2  nathanw 		}
    620  1.5.2.2  nathanw 	}
    621  1.5.2.2  nathanw 
    622  1.5.2.2  nathanw 	return 0;
    623  1.5.2.2  nathanw bad:
    624  1.5.2.2  nathanw 	printf("oboe: malloc for buffers failed()\n");
    625  1.5.2.2  nathanw 	return 1;
    626  1.5.2.2  nathanw }
    627  1.5.2.2  nathanw 
    628  1.5.2.2  nathanw static void
    629  1.5.2.2  nathanw oboe_startchip (struct oboe_softc *sc)
    630  1.5.2.2  nathanw {
    631  1.5.2.2  nathanw 	uint32_t physaddr;
    632  1.5.2.2  nathanw 
    633  1.5.2.2  nathanw 	OUTB(sc, 0, OBOE_LOCK);
    634  1.5.2.2  nathanw 	OUTB(sc, 0, OBOE_RST);
    635  1.5.2.2  nathanw 	OUTB(sc, OBOE_NTR_VAL, OBOE_NTR);
    636  1.5.2.2  nathanw 	OUTB(sc, 0xf0, OBOE_REG_D);
    637  1.5.2.2  nathanw 	OUTB(sc, 0xff, OBOE_ISR);
    638  1.5.2.2  nathanw 	OUTB(sc, 0x0f, OBOE_REG_1A);
    639  1.5.2.2  nathanw 	OUTB(sc, 0xff, OBOE_REG_1B);
    640  1.5.2.2  nathanw 
    641  1.5.2.2  nathanw 	physaddr = vtophys((u_int)sc->sc_taskfile); /* u_int? */
    642  1.5.2.2  nathanw 
    643  1.5.2.2  nathanw 	OUTB(sc, (physaddr >> 0x0a) & 0xff, OBOE_TFP0);
    644  1.5.2.2  nathanw 	OUTB(sc, (physaddr >> 0x12) & 0xff, OBOE_TFP1);
    645  1.5.2.2  nathanw 	OUTB(sc, (physaddr >> 0x1a) & 0x3f, OBOE_TFP2);
    646  1.5.2.2  nathanw 
    647  1.5.2.2  nathanw 	OUTB(sc, 0x0e, OBOE_REG_11);
    648  1.5.2.2  nathanw 	OUTB(sc, 0x80, OBOE_RST);
    649  1.5.2.2  nathanw 
    650  1.5.2.2  nathanw 	(void)oboe_setbaud(sc, 9600);
    651  1.5.2.2  nathanw 
    652  1.5.2.2  nathanw 	sc->sc_rxs = INB(sc, OBOE_RCVT);
    653  1.5.2.2  nathanw 	if (sc->sc_rxs < 0 || sc->sc_rxs >= RX_SLOTS)
    654  1.5.2.2  nathanw 		sc->sc_rxs = 0;
    655  1.5.2.2  nathanw 	sc->sc_txs = INB(sc, OBOE_XMTT) - OBOE_XMTT_OFFSET;
    656  1.5.2.2  nathanw 	if (sc->sc_txs < 0 || sc->sc_txs >= TX_SLOTS)
    657  1.5.2.2  nathanw 		sc->sc_txs = 0;
    658  1.5.2.2  nathanw }
    659  1.5.2.2  nathanw 
    660  1.5.2.2  nathanw static void
    661  1.5.2.2  nathanw oboe_stopchip (struct oboe_softc *sc)
    662  1.5.2.2  nathanw {
    663  1.5.2.2  nathanw 	OUTB(sc, 0x0e, OBOE_REG_11);
    664  1.5.2.2  nathanw 	OUTB(sc, 0x00, OBOE_RST);
    665  1.5.2.2  nathanw 	OUTB(sc, 0x3f, OBOE_TFP2);     /* Write the taskfile address */
    666  1.5.2.2  nathanw 	OUTB(sc, 0xff, OBOE_TFP1);
    667  1.5.2.2  nathanw 	OUTB(sc, 0xff, OBOE_TFP0);
    668  1.5.2.2  nathanw 	OUTB(sc, 0x0f, OBOE_REG_1B);
    669  1.5.2.2  nathanw 	OUTB(sc, 0xff, OBOE_REG_1A);
    670  1.5.2.2  nathanw 	OUTB(sc, 0x00, OBOE_ISR); /* XXX: should i do this to disable ints? */
    671  1.5.2.2  nathanw 	OUTB(sc, 0x80, OBOE_RST);
    672  1.5.2.2  nathanw 	OUTB(sc, 0x0e, OBOE_LOCK);
    673  1.5.2.2  nathanw }
    674  1.5.2.2  nathanw 
    675  1.5.2.2  nathanw #define SPEEDCASE(speed, type, divisor) \
    676  1.5.2.2  nathanw case speed: \
    677  1.5.2.2  nathanw OUTB(sc, OBOE_PMDL_##type, OBOE_PMDL); \
    678  1.5.2.2  nathanw OUTB(sc, OBOE_SMDL_##type, OBOE_SMDL); \
    679  1.5.2.2  nathanw OUTB(sc, divisor, OBOE_UDIV); \
    680  1.5.2.2  nathanw break
    681  1.5.2.2  nathanw 
    682  1.5.2.2  nathanw static int
    683  1.5.2.2  nathanw oboe_setbaud(struct oboe_softc *sc, int baud)
    684  1.5.2.2  nathanw {
    685  1.5.2.2  nathanw 	int s;
    686  1.5.2.2  nathanw 
    687  1.5.2.2  nathanw 	DPRINTF(("oboe: setting baud to %d\n", baud));
    688  1.5.2.2  nathanw 
    689  1.5.2.2  nathanw 	s = splir();
    690  1.5.2.2  nathanw 
    691  1.5.2.2  nathanw 	switch (baud) {
    692  1.5.2.2  nathanw 	SPEEDCASE(   2400, SIR, 0xbf);
    693  1.5.2.2  nathanw 	SPEEDCASE(   9600, SIR, 0x2f);
    694  1.5.2.2  nathanw 	SPEEDCASE(  19200, SIR, 0x17);
    695  1.5.2.2  nathanw 	SPEEDCASE(  38400, SIR, 0x0b);
    696  1.5.2.2  nathanw 	SPEEDCASE(  57600, SIR, 0x07);
    697  1.5.2.2  nathanw 	SPEEDCASE( 115200, SIR, 0x03);
    698  1.5.2.2  nathanw 	SPEEDCASE(1152000, MIR, 0x01);
    699  1.5.2.2  nathanw 	SPEEDCASE(4000000, FIR, 0x00);
    700  1.5.2.2  nathanw 	default:
    701  1.5.2.2  nathanw 		DPRINTF(("oboe: cannot set speed to %d\n", baud));
    702  1.5.2.2  nathanw 		splx(s);
    703  1.5.2.2  nathanw 		return (EINVAL);
    704  1.5.2.2  nathanw 	}
    705  1.5.2.2  nathanw 
    706  1.5.2.2  nathanw 	OUTB(sc, 0x00, OBOE_RST);
    707  1.5.2.2  nathanw 	OUTB(sc, 0x80, OBOE_RST);
    708  1.5.2.2  nathanw 	OUTB(sc, 0x01, OBOE_REG_9);
    709  1.5.2.2  nathanw 
    710  1.5.2.2  nathanw 	sc->sc_speed = baud;
    711  1.5.2.2  nathanw 
    712  1.5.2.2  nathanw 	splx(s);
    713  1.5.2.2  nathanw 
    714  1.5.2.2  nathanw 	return (0);
    715  1.5.2.2  nathanw }
    716