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