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