Home | History | Annotate | Line # | Download | only in pcmcia
btbc.c revision 1.1
      1  1.1  kiyohara /*	$NetBSD: btbc.c,v 1.1 2007/08/20 00:29:43 kiyohara Exp $	*/
      2  1.1  kiyohara /*
      3  1.1  kiyohara  * Copyright (c) 2007 KIYOHARA Takashi
      4  1.1  kiyohara  * All rights reserved.
      5  1.1  kiyohara  *
      6  1.1  kiyohara  * Redistribution and use in source and binary forms, with or without
      7  1.1  kiyohara  * modification, are permitted provided that the following conditions
      8  1.1  kiyohara  * are met:
      9  1.1  kiyohara  * 1. Redistributions of source code must retain the above copyright
     10  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer.
     11  1.1  kiyohara  * 2. Redistributions in binary form must reproduce the above copyright
     12  1.1  kiyohara  *    notice, this list of conditions and the following disclaimer in the
     13  1.1  kiyohara  *    documentation and/or other materials provided with the distribution.
     14  1.1  kiyohara  *
     15  1.1  kiyohara  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     16  1.1  kiyohara  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.1  kiyohara  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.1  kiyohara  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     19  1.1  kiyohara  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     20  1.1  kiyohara  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.1  kiyohara  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.1  kiyohara  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     23  1.1  kiyohara  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     24  1.1  kiyohara  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     25  1.1  kiyohara  * POSSIBILITY OF SUCH DAMAGE.
     26  1.1  kiyohara  */
     27  1.1  kiyohara /*
     28  1.1  kiyohara  * This driver is support to the AnyCom BlueCard.  written with reference to
     29  1.1  kiyohara  * Linux driver: (drivers/bluetooth/bluecard_cs.c)
     30  1.1  kiyohara  */
     31  1.1  kiyohara 
     32  1.1  kiyohara #include <sys/cdefs.h>
     33  1.1  kiyohara __KERNEL_RCSID(0, "$NetBSD: btbc.c,v 1.1 2007/08/20 00:29:43 kiyohara Exp $");
     34  1.1  kiyohara 
     35  1.1  kiyohara #include <sys/param.h>
     36  1.1  kiyohara #include <sys/callout.h>
     37  1.1  kiyohara #include <sys/device.h>
     38  1.1  kiyohara #include <sys/errno.h>
     39  1.1  kiyohara #include <sys/kernel.h>
     40  1.1  kiyohara #include <sys/mbuf.h>
     41  1.1  kiyohara #include <sys/proc.h>
     42  1.1  kiyohara 
     43  1.1  kiyohara #include <machine/bus.h>
     44  1.1  kiyohara #include <machine/intr.h>
     45  1.1  kiyohara 
     46  1.1  kiyohara #include <dev/pcmcia/pcmciareg.h>
     47  1.1  kiyohara #include <dev/pcmcia/pcmciavar.h>
     48  1.1  kiyohara #include <dev/pcmcia/pcmciadevs.h>
     49  1.1  kiyohara 
     50  1.1  kiyohara #include <netbt/bluetooth.h>
     51  1.1  kiyohara #include <netbt/hci.h>
     52  1.1  kiyohara 
     53  1.1  kiyohara #include <dev/pcmcia/bluecardreg.h>
     54  1.1  kiyohara 
     55  1.1  kiyohara 
     56  1.1  kiyohara /* sc_state */				/* receiving */
     57  1.1  kiyohara #define BTBC_RECV_PKT_TYPE	0		/* packet type */
     58  1.1  kiyohara #define BTBC_RECV_ACL_HDR	1		/* acl header */
     59  1.1  kiyohara #define BTBC_RECV_SCO_HDR	2		/* sco header */
     60  1.1  kiyohara #define BTBC_RECV_EVENT_HDR	3		/* event header */
     61  1.1  kiyohara #define BTBC_RECV_ACL_DATA	4		/* acl packet data */
     62  1.1  kiyohara #define BTBC_RECV_SCO_DATA	5		/* sco packet data */
     63  1.1  kiyohara #define BTBC_RECV_EVENT_DATA	6		/* event packet data */
     64  1.1  kiyohara 
     65  1.1  kiyohara /* sc_flags */
     66  1.1  kiyohara #define BTBC_SLEEPING		(1 << 0)	/* but not with the fishes */
     67  1.1  kiyohara 
     68  1.1  kiyohara /* Default baud rate: 57600, 115200, 230400 or 460800 */
     69  1.1  kiyohara #define BTBC_DEFAULT_BAUDRATE	230400
     70  1.1  kiyohara 
     71  1.1  kiyohara struct btbc_softc {
     72  1.1  kiyohara 	struct device sc_dev;			/* required */
     73  1.1  kiyohara 
     74  1.1  kiyohara 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     75  1.1  kiyohara 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     76  1.1  kiyohara 	void *sc_powerhook;			/* power hook descriptor */
     77  1.1  kiyohara 	int sc_flags;				/* flags */
     78  1.1  kiyohara 
     79  1.1  kiyohara 	struct hci_unit sc_unit;		/* Bluetooth HCI Unit */
     80  1.1  kiyohara 
     81  1.1  kiyohara 	/* hardware interrupt */
     82  1.1  kiyohara 	void *sc_intr;				/* cookie */
     83  1.1  kiyohara 	int sc_state;				/* receive state */
     84  1.1  kiyohara 	int sc_want;				/* how much we want */
     85  1.1  kiyohara 	struct mbuf *sc_rxp;			/* incoming packet */
     86  1.1  kiyohara 	struct mbuf *sc_txp;			/* outgoing packet */
     87  1.1  kiyohara 	int sc_txstate;
     88  1.1  kiyohara #define TXBUF1_EMPTY	(1 << 0)
     89  1.1  kiyohara #define TXBUF2_EMPTY	(1 << 1)
     90  1.1  kiyohara #define TXBUF_MASK	(1 << 2)
     91  1.1  kiyohara 
     92  1.1  kiyohara 	callout_t sc_ledch;			/* callout handler for LED */
     93  1.1  kiyohara 	uint8_t sc_ctrlreg;			/* value for control register */
     94  1.1  kiyohara };
     95  1.1  kiyohara 
     96  1.1  kiyohara static int btbc_match(struct device *, struct cfdata *, void *);
     97  1.1  kiyohara static void btbc_attach(struct device *, struct device *, void *);
     98  1.1  kiyohara static int btbc_detach(struct device *, int);
     99  1.1  kiyohara static int btbc_activate(struct device *, enum devact);
    100  1.1  kiyohara static void btbc_power(int, void *);
    101  1.1  kiyohara 
    102  1.1  kiyohara static void btbc_activity_led_timeout(void *);
    103  1.1  kiyohara static void btbc_enable_activity_led(struct btbc_softc *);
    104  1.1  kiyohara static int btbc_read(struct btbc_softc *, uint32_t, uint8_t *, int);
    105  1.1  kiyohara static int btbc_write(struct btbc_softc *, uint32_t, uint8_t *, int);
    106  1.1  kiyohara static int btbc_set_baudrate(struct btbc_softc *, int);
    107  1.1  kiyohara static void btbc_receive(struct btbc_softc *, uint32_t);
    108  1.1  kiyohara static void btbc_transmit(struct btbc_softc *);
    109  1.1  kiyohara static int btbc_intr(void *);
    110  1.1  kiyohara 
    111  1.1  kiyohara static void btbc_start(struct hci_unit *);
    112  1.1  kiyohara static int btbc_enable(struct hci_unit *);
    113  1.1  kiyohara static void btbc_disable(struct hci_unit *);
    114  1.1  kiyohara 
    115  1.1  kiyohara CFATTACH_DECL(btbc, sizeof(struct btbc_softc),
    116  1.1  kiyohara     btbc_match, btbc_attach, btbc_detach, btbc_activate);
    117  1.1  kiyohara 
    118  1.1  kiyohara 
    119  1.1  kiyohara /* ARGSUSED */
    120  1.1  kiyohara static int
    121  1.1  kiyohara btbc_match(struct device *parent, struct cfdata *match, void *aux)
    122  1.1  kiyohara {
    123  1.1  kiyohara 	struct pcmcia_attach_args *pa = aux;
    124  1.1  kiyohara 
    125  1.1  kiyohara 	if (pa->manufacturer == PCMCIA_VENDOR_ANYCOM)
    126  1.1  kiyohara 		if ((pa->product == PCMCIA_PRODUCT_ANYCOM_LSE041) ||
    127  1.1  kiyohara 		    (pa->product == PCMCIA_PRODUCT_ANYCOM_LSE039) ||
    128  1.1  kiyohara 		    (pa->product == PCMCIA_PRODUCT_ANYCOM_LSE139))
    129  1.1  kiyohara 			return 1;
    130  1.1  kiyohara 	return 0;
    131  1.1  kiyohara }
    132  1.1  kiyohara 
    133  1.1  kiyohara static int
    134  1.1  kiyohara btbc_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
    135  1.1  kiyohara {
    136  1.1  kiyohara 
    137  1.1  kiyohara 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    138  1.1  kiyohara 	    cfe->num_iospace < 1 || cfe->num_iospace > 2)
    139  1.1  kiyohara 		return EINVAL;
    140  1.1  kiyohara 	return 0;
    141  1.1  kiyohara }
    142  1.1  kiyohara 
    143  1.1  kiyohara /* ARGSUSED */
    144  1.1  kiyohara static void
    145  1.1  kiyohara btbc_attach(struct device *parent, struct device *self, void *aux)
    146  1.1  kiyohara {
    147  1.1  kiyohara 	struct btbc_softc *sc = (struct btbc_softc *)self;
    148  1.1  kiyohara 	struct pcmcia_attach_args *pa = aux;
    149  1.1  kiyohara 	struct pcmcia_config_entry *cfe;
    150  1.1  kiyohara 	int error;
    151  1.1  kiyohara 
    152  1.1  kiyohara 	sc->sc_pf = pa->pf;
    153  1.1  kiyohara 
    154  1.1  kiyohara 	if ((error = pcmcia_function_configure(pa->pf,
    155  1.1  kiyohara 	    btbc_pcmcia_validate_config)) != 0) {
    156  1.1  kiyohara 		aprint_error("%s: configure failed, error=%d\n",
    157  1.1  kiyohara 		    self->dv_xname, error);
    158  1.1  kiyohara 		return;
    159  1.1  kiyohara 	}
    160  1.1  kiyohara 
    161  1.1  kiyohara 	cfe = pa->pf->cfe;
    162  1.1  kiyohara 	sc->sc_pcioh = cfe->iospace[0].handle;
    163  1.1  kiyohara 
    164  1.1  kiyohara 	/* Attach Bluetooth unit */
    165  1.1  kiyohara 	sc->sc_unit.hci_softc = sc;
    166  1.1  kiyohara 	sc->sc_unit.hci_devname = sc->sc_dev.dv_xname;
    167  1.1  kiyohara 	sc->sc_unit.hci_enable = btbc_enable;
    168  1.1  kiyohara 	sc->sc_unit.hci_disable = btbc_disable;
    169  1.1  kiyohara 	sc->sc_unit.hci_start_cmd = btbc_start;
    170  1.1  kiyohara 	sc->sc_unit.hci_start_acl = btbc_start;
    171  1.1  kiyohara 	sc->sc_unit.hci_start_sco = btbc_start;
    172  1.1  kiyohara 	sc->sc_unit.hci_ipl = makeiplcookie(IPL_TTY);
    173  1.1  kiyohara 	hci_attach(&sc->sc_unit);
    174  1.1  kiyohara 
    175  1.1  kiyohara 	/* establish a power change hook */
    176  1.1  kiyohara 	sc->sc_powerhook = powerhook_establish(sc->sc_dev.dv_xname,
    177  1.1  kiyohara 	    btbc_power, sc);
    178  1.1  kiyohara 
    179  1.1  kiyohara 	callout_init(&sc->sc_ledch, 0);
    180  1.1  kiyohara 
    181  1.1  kiyohara 	return;
    182  1.1  kiyohara }
    183  1.1  kiyohara 
    184  1.1  kiyohara /* ARGSUSED */
    185  1.1  kiyohara static int
    186  1.1  kiyohara btbc_detach(struct device *self, int flags)
    187  1.1  kiyohara {
    188  1.1  kiyohara 	struct btbc_softc *sc = (struct btbc_softc *)self;
    189  1.1  kiyohara 	int err = 0;
    190  1.1  kiyohara 
    191  1.1  kiyohara 	btbc_disable(&sc->sc_unit);
    192  1.1  kiyohara 
    193  1.1  kiyohara 	if (sc->sc_powerhook) {
    194  1.1  kiyohara 		powerhook_disestablish(sc->sc_powerhook);
    195  1.1  kiyohara 		sc->sc_powerhook = NULL;
    196  1.1  kiyohara 	}
    197  1.1  kiyohara 
    198  1.1  kiyohara 	callout_stop(&sc->sc_ledch);
    199  1.1  kiyohara 
    200  1.1  kiyohara 	hci_detach(&sc->sc_unit);
    201  1.1  kiyohara 
    202  1.1  kiyohara 	pcmcia_function_unconfigure(sc->sc_pf);
    203  1.1  kiyohara 
    204  1.1  kiyohara 	return err;
    205  1.1  kiyohara }
    206  1.1  kiyohara 
    207  1.1  kiyohara /* ARGSUSED */
    208  1.1  kiyohara static int
    209  1.1  kiyohara btbc_activate(struct device *self, enum devact act)
    210  1.1  kiyohara {
    211  1.1  kiyohara 	int err = 0;
    212  1.1  kiyohara 
    213  1.1  kiyohara 	switch(act) {
    214  1.1  kiyohara 	case DVACT_ACTIVATE:
    215  1.1  kiyohara 		err = EOPNOTSUPP;
    216  1.1  kiyohara 		break;
    217  1.1  kiyohara 
    218  1.1  kiyohara 	case DVACT_DEACTIVATE:
    219  1.1  kiyohara 		// could notify unit somehow?
    220  1.1  kiyohara 		break;
    221  1.1  kiyohara 	}
    222  1.1  kiyohara 
    223  1.1  kiyohara 	return err;
    224  1.1  kiyohara }
    225  1.1  kiyohara 
    226  1.1  kiyohara static void
    227  1.1  kiyohara btbc_power(int why, void *arg)
    228  1.1  kiyohara {
    229  1.1  kiyohara 	struct btbc_softc *sc = arg;
    230  1.1  kiyohara 
    231  1.1  kiyohara 	switch(why) {
    232  1.1  kiyohara 	case PWR_SUSPEND:
    233  1.1  kiyohara 	case PWR_STANDBY:
    234  1.1  kiyohara 		if (sc->sc_unit.hci_flags & BTF_RUNNING) {
    235  1.1  kiyohara 			hci_detach(&sc->sc_unit);
    236  1.1  kiyohara 
    237  1.1  kiyohara 			sc->sc_flags |= BTBC_SLEEPING;
    238  1.1  kiyohara 			printf_nolog("%s: sleeping\n", sc->sc_dev.dv_xname);
    239  1.1  kiyohara 		}
    240  1.1  kiyohara 		break;
    241  1.1  kiyohara 
    242  1.1  kiyohara 	case PWR_RESUME:
    243  1.1  kiyohara 		if (sc->sc_flags & BTBC_SLEEPING) {
    244  1.1  kiyohara 			printf_nolog("%s: waking up\n", sc->sc_dev.dv_xname);
    245  1.1  kiyohara 			sc->sc_flags &= ~BTBC_SLEEPING;
    246  1.1  kiyohara 
    247  1.1  kiyohara 			memset(&sc->sc_unit, 0, sizeof(sc->sc_unit));
    248  1.1  kiyohara 			sc->sc_unit.hci_softc = sc;
    249  1.1  kiyohara 			sc->sc_unit.hci_devname = sc->sc_dev.dv_xname;
    250  1.1  kiyohara 			sc->sc_unit.hci_enable = btbc_enable;
    251  1.1  kiyohara 			sc->sc_unit.hci_disable = btbc_disable;
    252  1.1  kiyohara 			sc->sc_unit.hci_start_cmd = btbc_start;
    253  1.1  kiyohara 			sc->sc_unit.hci_start_acl = btbc_start;
    254  1.1  kiyohara 			sc->sc_unit.hci_start_sco = btbc_start;
    255  1.1  kiyohara 			sc->sc_unit.hci_ipl = makeiplcookie(IPL_TTY);
    256  1.1  kiyohara 			hci_attach(&sc->sc_unit);
    257  1.1  kiyohara 		}
    258  1.1  kiyohara 		break;
    259  1.1  kiyohara 
    260  1.1  kiyohara 	case PWR_SOFTSUSPEND:
    261  1.1  kiyohara 	case PWR_SOFTSTANDBY:
    262  1.1  kiyohara 	case PWR_SOFTRESUME:
    263  1.1  kiyohara 		break;
    264  1.1  kiyohara 	}
    265  1.1  kiyohara }
    266  1.1  kiyohara 
    267  1.1  kiyohara static void
    268  1.1  kiyohara btbc_activity_led_timeout(void *arg)
    269  1.1  kiyohara {
    270  1.1  kiyohara 	struct btbc_softc *sc = arg;
    271  1.1  kiyohara 	uint8_t id;
    272  1.1  kiyohara 
    273  1.1  kiyohara 	id = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    274  1.1  kiyohara 	    BLUECARD_LEDCONTROL);
    275  1.1  kiyohara 	if (id & 0x20)
    276  1.1  kiyohara 		/* Disable activity LED */
    277  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    278  1.1  kiyohara 		    BLUECARD_LEDCONTROL, 0x08 | 0x20);
    279  1.1  kiyohara 	else
    280  1.1  kiyohara 		/* Disable power LED */
    281  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    282  1.1  kiyohara 		    BLUECARD_LEDCONTROL, 0x00);
    283  1.1  kiyohara }
    284  1.1  kiyohara 
    285  1.1  kiyohara static void
    286  1.1  kiyohara btbc_enable_activity_led(struct btbc_softc *sc)
    287  1.1  kiyohara {
    288  1.1  kiyohara 	uint8_t id;
    289  1.1  kiyohara 
    290  1.1  kiyohara 	id = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    291  1.1  kiyohara 	    BLUECARD_LEDCONTROL);
    292  1.1  kiyohara 	if (id & 0x20) {
    293  1.1  kiyohara 		/* Enable activity LED */
    294  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    295  1.1  kiyohara 		    BLUECARD_LEDCONTROL, 0x10 | 0x40);
    296  1.1  kiyohara 
    297  1.1  kiyohara 		/* Stop the LED after hz/4 */
    298  1.1  kiyohara 		callout_reset(&sc->sc_ledch, hz / 4,
    299  1.1  kiyohara 		    btbc_activity_led_timeout, sc);
    300  1.1  kiyohara 	} else {
    301  1.1  kiyohara 		/* Enable power LED */
    302  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    303  1.1  kiyohara 		    BLUECARD_LEDCONTROL, 0x08 | 0x20);
    304  1.1  kiyohara 
    305  1.1  kiyohara 		/* Stop the LED after HZ/2 */
    306  1.1  kiyohara 		callout_reset(&sc->sc_ledch, hz / 2,
    307  1.1  kiyohara 		    btbc_activity_led_timeout, sc);
    308  1.1  kiyohara 	}
    309  1.1  kiyohara }
    310  1.1  kiyohara 
    311  1.1  kiyohara static int
    312  1.1  kiyohara btbc_read(struct btbc_softc *sc, uint32_t offset, uint8_t *buf, int buflen)
    313  1.1  kiyohara {
    314  1.1  kiyohara 	int i, n, len;
    315  1.1  kiyohara 
    316  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    317  1.1  kiyohara 	    BLUECARD_COMMAND, BLUECARD_COMMAND_RXWIN1);
    318  1.1  kiyohara 	len = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, offset);
    319  1.1  kiyohara 
    320  1.1  kiyohara 	n = 0;
    321  1.1  kiyohara 	i = 1;
    322  1.1  kiyohara 	while (n < len) {
    323  1.1  kiyohara 		if (i == 16) {
    324  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    325  1.1  kiyohara 			    BLUECARD_COMMAND, BLUECARD_COMMAND_RXWIN2);
    326  1.1  kiyohara 			i = 0;
    327  1.1  kiyohara 		}
    328  1.1  kiyohara 
    329  1.1  kiyohara 		buf[n] = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    330  1.1  kiyohara 		    offset + i);
    331  1.1  kiyohara 		i++;
    332  1.1  kiyohara 		if (++n > buflen)
    333  1.1  kiyohara 			break;
    334  1.1  kiyohara 	}
    335  1.1  kiyohara 	return len;
    336  1.1  kiyohara }
    337  1.1  kiyohara 
    338  1.1  kiyohara static int
    339  1.1  kiyohara btbc_write(struct btbc_softc *sc, uint32_t offset, uint8_t *buf, int buflen)
    340  1.1  kiyohara {
    341  1.1  kiyohara         int i, actual;
    342  1.1  kiyohara 
    343  1.1  kiyohara 	actual = (buflen > 15) ? 15 : buflen;
    344  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, offset, actual);
    345  1.1  kiyohara 	for (i = 0; i < actual; i++)
    346  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    347  1.1  kiyohara 		    offset + i + 1, buf[i]);
    348  1.1  kiyohara 	return actual;
    349  1.1  kiyohara }
    350  1.1  kiyohara 
    351  1.1  kiyohara /*
    352  1.1  kiyohara  * send Ericsson baud rate command
    353  1.1  kiyohara  */
    354  1.1  kiyohara static int
    355  1.1  kiyohara btbc_set_baudrate(struct btbc_softc *sc, int baud)
    356  1.1  kiyohara {
    357  1.1  kiyohara 	struct hci_unit *unit = &sc->sc_unit;
    358  1.1  kiyohara 	hci_cmd_hdr_t *p;
    359  1.1  kiyohara 	struct mbuf *m;
    360  1.1  kiyohara 	const uint16_t opcode = htole16(HCI_CMD_ERICSSON_SET_UART_BAUD_RATE);
    361  1.1  kiyohara 	uint8_t param;
    362  1.1  kiyohara 
    363  1.1  kiyohara 	m = m_gethdr(M_WAIT, MT_DATA);
    364  1.1  kiyohara 	if (m == NULL)
    365  1.1  kiyohara 		return ENOMEM;
    366  1.1  kiyohara 
    367  1.1  kiyohara 	switch (baud) {
    368  1.1  kiyohara 	case 460800:
    369  1.1  kiyohara 		param = 0x00;
    370  1.1  kiyohara 		break;
    371  1.1  kiyohara 
    372  1.1  kiyohara 	case 230400:
    373  1.1  kiyohara 		param = 0x01;
    374  1.1  kiyohara 		break;
    375  1.1  kiyohara 
    376  1.1  kiyohara         case 115200:
    377  1.1  kiyohara 		param = 0x02;
    378  1.1  kiyohara 		break;
    379  1.1  kiyohara 
    380  1.1  kiyohara 	case 57600:
    381  1.1  kiyohara 	default:
    382  1.1  kiyohara 		param = 0x03;
    383  1.1  kiyohara 		break;
    384  1.1  kiyohara 	}
    385  1.1  kiyohara 
    386  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    387  1.1  kiyohara 	p->type = HCI_CMD_PKT;
    388  1.1  kiyohara 	p->opcode = opcode;
    389  1.1  kiyohara 	p->length = sizeof(param);
    390  1.1  kiyohara 	m->m_pkthdr.len = m->m_len = sizeof(hci_cmd_hdr_t);
    391  1.1  kiyohara 	m_copyback(m, sizeof(hci_cmd_hdr_t), p->length, &param);
    392  1.1  kiyohara 
    393  1.1  kiyohara 	MBUFQ_ENQUEUE(&unit->hci_cmdq, m);
    394  1.1  kiyohara 	btbc_start(unit);
    395  1.1  kiyohara 
    396  1.1  kiyohara 	return 0;
    397  1.1  kiyohara }
    398  1.1  kiyohara 
    399  1.1  kiyohara static void
    400  1.1  kiyohara btbc_receive(struct btbc_softc *sc, uint32_t offset)
    401  1.1  kiyohara {
    402  1.1  kiyohara 	struct mbuf *m = sc->sc_rxp;
    403  1.1  kiyohara 	int count, space = 0, i;
    404  1.1  kiyohara 	uint8_t buf[31];
    405  1.1  kiyohara 
    406  1.1  kiyohara 	btbc_enable_activity_led(sc);
    407  1.1  kiyohara 
    408  1.1  kiyohara 	/*
    409  1.1  kiyohara 	 * If we already started a packet, find the
    410  1.1  kiyohara 	 * trailing end of it.
    411  1.1  kiyohara 	 */
    412  1.1  kiyohara 	if (m) {
    413  1.1  kiyohara 		while (m->m_next)
    414  1.1  kiyohara 			m = m->m_next;
    415  1.1  kiyohara 
    416  1.1  kiyohara 		space = M_TRAILINGSPACE(m);
    417  1.1  kiyohara 	}
    418  1.1  kiyohara 
    419  1.1  kiyohara 	count = btbc_read(sc, offset, buf, sizeof(buf));
    420  1.1  kiyohara 	i = 0;
    421  1.1  kiyohara 
    422  1.1  kiyohara 	while (i < count) {
    423  1.1  kiyohara 		if (space == 0) {
    424  1.1  kiyohara 			if (m == NULL) {
    425  1.1  kiyohara 				/* new packet */
    426  1.1  kiyohara 				MGETHDR(m, M_DONTWAIT, MT_DATA);
    427  1.1  kiyohara 				if (m == NULL) {
    428  1.1  kiyohara 					printf("%s: out of memory\n",
    429  1.1  kiyohara 						sc->sc_dev.dv_xname);
    430  1.1  kiyohara 					++sc->sc_unit.hci_stats.err_rx;
    431  1.1  kiyohara 					return;		/* (lost sync) */
    432  1.1  kiyohara 				}
    433  1.1  kiyohara 
    434  1.1  kiyohara 				sc->sc_rxp = m;
    435  1.1  kiyohara 				m->m_pkthdr.len = m->m_len = 0;
    436  1.1  kiyohara 				space = MHLEN;
    437  1.1  kiyohara 
    438  1.1  kiyohara 				sc->sc_state = BTBC_RECV_PKT_TYPE;
    439  1.1  kiyohara 				sc->sc_want = 1;
    440  1.1  kiyohara 			} else {
    441  1.1  kiyohara 				/* extend mbuf */
    442  1.1  kiyohara 				MGET(m->m_next, M_DONTWAIT, MT_DATA);
    443  1.1  kiyohara 				if (m->m_next == NULL) {
    444  1.1  kiyohara 					printf("%s: out of memory\n",
    445  1.1  kiyohara 						sc->sc_dev.dv_xname);
    446  1.1  kiyohara 					++sc->sc_unit.hci_stats.err_rx;
    447  1.1  kiyohara 					return;		/* (lost sync) */
    448  1.1  kiyohara 				}
    449  1.1  kiyohara 
    450  1.1  kiyohara 				m = m->m_next;
    451  1.1  kiyohara 				m->m_len = 0;
    452  1.1  kiyohara 				space = MLEN;
    453  1.1  kiyohara 
    454  1.1  kiyohara 				if (sc->sc_want > MINCLSIZE) {
    455  1.1  kiyohara 					MCLGET(m, M_DONTWAIT);
    456  1.1  kiyohara 					if (m->m_flags & M_EXT)
    457  1.1  kiyohara 						space = MCLBYTES;
    458  1.1  kiyohara 				}
    459  1.1  kiyohara 			}
    460  1.1  kiyohara 		}
    461  1.1  kiyohara 
    462  1.1  kiyohara 		mtod(m, uint8_t *)[m->m_len++] = buf[i];
    463  1.1  kiyohara 		space--;
    464  1.1  kiyohara 		sc->sc_rxp->m_pkthdr.len++;
    465  1.1  kiyohara 		sc->sc_unit.hci_stats.byte_rx++;
    466  1.1  kiyohara 
    467  1.1  kiyohara 		sc->sc_want--;
    468  1.1  kiyohara 		if (sc->sc_want > 0) {
    469  1.1  kiyohara 			i++;
    470  1.1  kiyohara 			continue; /* want more */
    471  1.1  kiyohara 		}
    472  1.1  kiyohara 
    473  1.1  kiyohara 		switch (sc->sc_state) {
    474  1.1  kiyohara 		case BTBC_RECV_PKT_TYPE:		/* Got packet type */
    475  1.1  kiyohara 			switch (buf[i]) {
    476  1.1  kiyohara 			case 0x00:	/* init packet */
    477  1.1  kiyohara 				m_freem(sc->sc_rxp);
    478  1.1  kiyohara 				sc->sc_rxp = NULL;
    479  1.1  kiyohara 				break;
    480  1.1  kiyohara 
    481  1.1  kiyohara 			case HCI_ACL_DATA_PKT:
    482  1.1  kiyohara 				sc->sc_state = BTBC_RECV_ACL_HDR;
    483  1.1  kiyohara 				sc->sc_want = sizeof(hci_acldata_hdr_t) - 1;
    484  1.1  kiyohara 				break;
    485  1.1  kiyohara 
    486  1.1  kiyohara 			case HCI_SCO_DATA_PKT:
    487  1.1  kiyohara 				sc->sc_state = BTBC_RECV_SCO_HDR;
    488  1.1  kiyohara 				sc->sc_want = sizeof(hci_scodata_hdr_t) - 1;
    489  1.1  kiyohara 				break;
    490  1.1  kiyohara 
    491  1.1  kiyohara 			case HCI_EVENT_PKT:
    492  1.1  kiyohara 				sc->sc_state = BTBC_RECV_EVENT_HDR;
    493  1.1  kiyohara 				sc->sc_want = sizeof(hci_event_hdr_t) - 1;
    494  1.1  kiyohara 				break;
    495  1.1  kiyohara 
    496  1.1  kiyohara 			default:
    497  1.1  kiyohara 				printf("%s: Unknown packet type=%#x!\n",
    498  1.1  kiyohara 					sc->sc_dev.dv_xname, buf[i]);
    499  1.1  kiyohara 				++sc->sc_unit.hci_stats.err_rx;
    500  1.1  kiyohara 				m_freem(sc->sc_rxp);
    501  1.1  kiyohara 				sc->sc_rxp = NULL;
    502  1.1  kiyohara 				return;		/* (lost sync) */
    503  1.1  kiyohara 			}
    504  1.1  kiyohara 
    505  1.1  kiyohara 			break;
    506  1.1  kiyohara 
    507  1.1  kiyohara 		/*
    508  1.1  kiyohara 		 * we assume (correctly of course :) that the packet headers
    509  1.1  kiyohara 		 * all fit into a single pkthdr mbuf
    510  1.1  kiyohara 		 */
    511  1.1  kiyohara 		case BTBC_RECV_ACL_HDR:		/* Got ACL Header */
    512  1.1  kiyohara 			sc->sc_state = BTBC_RECV_ACL_DATA;
    513  1.1  kiyohara 			sc->sc_want = mtod(m, hci_acldata_hdr_t *)->length;
    514  1.1  kiyohara 			sc->sc_want = le16toh(sc->sc_want);
    515  1.1  kiyohara 			break;
    516  1.1  kiyohara 
    517  1.1  kiyohara 		case BTBC_RECV_SCO_HDR:		/* Got SCO Header */
    518  1.1  kiyohara 			sc->sc_state = BTBC_RECV_SCO_DATA;
    519  1.1  kiyohara 			sc->sc_want =  mtod(m, hci_scodata_hdr_t *)->length;
    520  1.1  kiyohara 			break;
    521  1.1  kiyohara 
    522  1.1  kiyohara 		case BTBC_RECV_EVENT_HDR:	/* Got Event Header */
    523  1.1  kiyohara 			sc->sc_state = BTBC_RECV_EVENT_DATA;
    524  1.1  kiyohara 			sc->sc_want =  mtod(m, hci_event_hdr_t *)->length;
    525  1.1  kiyohara 			break;
    526  1.1  kiyohara 
    527  1.1  kiyohara 		case BTBC_RECV_ACL_DATA:	/* ACL Packet Complete */
    528  1.1  kiyohara 			hci_input_acl(&sc->sc_unit, sc->sc_rxp);
    529  1.1  kiyohara 			sc->sc_unit.hci_stats.acl_rx++;
    530  1.1  kiyohara 			sc->sc_rxp = m = NULL;
    531  1.1  kiyohara 			space = 0;
    532  1.1  kiyohara 			break;
    533  1.1  kiyohara 
    534  1.1  kiyohara 		case BTBC_RECV_SCO_DATA:	/* SCO Packet Complete */
    535  1.1  kiyohara 			hci_input_sco(&sc->sc_unit, sc->sc_rxp);
    536  1.1  kiyohara 			sc->sc_unit.hci_stats.sco_rx++;
    537  1.1  kiyohara 			sc->sc_rxp = m = NULL;
    538  1.1  kiyohara 			space = 0;
    539  1.1  kiyohara 			break;
    540  1.1  kiyohara 
    541  1.1  kiyohara 		case BTBC_RECV_EVENT_DATA:	/* Event Packet Complete */
    542  1.1  kiyohara 			sc->sc_unit.hci_stats.evt_rx++;
    543  1.1  kiyohara 			hci_input_event(&sc->sc_unit, sc->sc_rxp);
    544  1.1  kiyohara 			sc->sc_rxp = m = NULL;
    545  1.1  kiyohara 			space = 0;
    546  1.1  kiyohara 			break;
    547  1.1  kiyohara 
    548  1.1  kiyohara 		default:
    549  1.1  kiyohara 			panic("%s: invalid state %d!\n",
    550  1.1  kiyohara 				sc->sc_dev.dv_xname, sc->sc_state);
    551  1.1  kiyohara 		}
    552  1.1  kiyohara 		i++;
    553  1.1  kiyohara 	}
    554  1.1  kiyohara }
    555  1.1  kiyohara 
    556  1.1  kiyohara /*
    557  1.1  kiyohara  * write data from current packet to Transmit FIFO.
    558  1.1  kiyohara  * restart when done.
    559  1.1  kiyohara  */
    560  1.1  kiyohara static void
    561  1.1  kiyohara btbc_transmit(struct btbc_softc *sc)
    562  1.1  kiyohara {
    563  1.1  kiyohara 	hci_cmd_hdr_t *p;
    564  1.1  kiyohara 	struct mbuf *m;
    565  1.1  kiyohara 	int count, rlen, set_baudrate, n, s;
    566  1.1  kiyohara 	uint32_t offset, command;
    567  1.1  kiyohara 	uint8_t *rptr;
    568  1.1  kiyohara 
    569  1.1  kiyohara 	m = sc->sc_txp;
    570  1.1  kiyohara 	if (m == NULL) {
    571  1.1  kiyohara 		sc->sc_unit.hci_flags &= ~BTF_XMIT;
    572  1.1  kiyohara 		btbc_start(&sc->sc_unit);
    573  1.1  kiyohara 		return;
    574  1.1  kiyohara 	}
    575  1.1  kiyohara 
    576  1.1  kiyohara 	set_baudrate = 0;
    577  1.1  kiyohara 	p = mtod(m, hci_cmd_hdr_t *);
    578  1.1  kiyohara 	if ((void *)m->m_pktdat == (void *)p) {
    579  1.1  kiyohara 		const uint16_t opcode =
    580  1.1  kiyohara 		    htole16(HCI_CMD_ERICSSON_SET_UART_BAUD_RATE);
    581  1.1  kiyohara 
    582  1.1  kiyohara 		if (p->type == HCI_CMD_PKT &&
    583  1.1  kiyohara 		    p->opcode == opcode &&
    584  1.1  kiyohara 		    p->length == 1) {
    585  1.1  kiyohara 			set_baudrate = 1;
    586  1.1  kiyohara 			sc->sc_txp = NULL;	/* safe reentrant */
    587  1.1  kiyohara 		}
    588  1.1  kiyohara 	}
    589  1.1  kiyohara 
    590  1.1  kiyohara 	count = 0;
    591  1.1  kiyohara 	rlen = 0;
    592  1.1  kiyohara 	rptr = mtod(m, uint8_t *);
    593  1.1  kiyohara 	for(;;) {
    594  1.1  kiyohara 		if (rlen >= m->m_len) {
    595  1.1  kiyohara 			m = m->m_next;
    596  1.1  kiyohara 			if (m == NULL) {
    597  1.1  kiyohara 				m = sc->sc_txp;
    598  1.1  kiyohara 				sc->sc_txp = NULL;
    599  1.1  kiyohara 
    600  1.1  kiyohara 				if (M_GETCTX(m, void *) == NULL)
    601  1.1  kiyohara 					m_freem(m);
    602  1.1  kiyohara 				else
    603  1.1  kiyohara 					hci_complete_sco(&sc->sc_unit, m);
    604  1.1  kiyohara 
    605  1.1  kiyohara 				break;
    606  1.1  kiyohara 			}
    607  1.1  kiyohara 
    608  1.1  kiyohara 			rlen = 0;
    609  1.1  kiyohara 			rptr = mtod(m, uint8_t *);
    610  1.1  kiyohara 			continue;
    611  1.1  kiyohara 		}
    612  1.1  kiyohara 
    613  1.1  kiyohara 		s = splhigh();
    614  1.1  kiyohara 		if (sc->sc_txstate & TXBUF_MASK) {
    615  1.1  kiyohara 			if (sc->sc_txstate & TXBUF2_EMPTY) {
    616  1.1  kiyohara 				offset = BLUECARD_BUF2;
    617  1.1  kiyohara 				command = BLUECARD_COMMAND_TXBUF2;
    618  1.1  kiyohara 				sc->sc_txstate &= ~(TXBUF2_EMPTY | TXBUF_MASK);
    619  1.1  kiyohara 			} else {
    620  1.1  kiyohara 				splx(s);
    621  1.1  kiyohara 				m_adj(m, rlen);
    622  1.1  kiyohara 				break;
    623  1.1  kiyohara 			}
    624  1.1  kiyohara 		} else {
    625  1.1  kiyohara 			if (sc->sc_txstate & TXBUF1_EMPTY) {
    626  1.1  kiyohara 				offset = BLUECARD_BUF1;
    627  1.1  kiyohara 				command = BLUECARD_COMMAND_TXBUF1;
    628  1.1  kiyohara 				sc->sc_txstate &= ~TXBUF1_EMPTY;
    629  1.1  kiyohara 				sc->sc_txstate |= TXBUF_MASK;
    630  1.1  kiyohara 			} else {
    631  1.1  kiyohara 				splx(s);
    632  1.1  kiyohara 				m_adj(m, rlen);
    633  1.1  kiyohara 				break;
    634  1.1  kiyohara 			}
    635  1.1  kiyohara 		}
    636  1.1  kiyohara 		splx(s);
    637  1.1  kiyohara 
    638  1.1  kiyohara 		if (set_baudrate) {
    639  1.1  kiyohara 			/* Disable RTS */
    640  1.1  kiyohara 			sc->sc_ctrlreg |= BLUECARD_CONTROL_RTS;
    641  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    642  1.1  kiyohara 			    BLUECARD_CONTROL, sc->sc_ctrlreg);
    643  1.1  kiyohara 		}
    644  1.1  kiyohara 
    645  1.1  kiyohara 		/* Activate LED */
    646  1.1  kiyohara 		btbc_enable_activity_led(sc);
    647  1.1  kiyohara 
    648  1.1  kiyohara 		/* Send frame */
    649  1.1  kiyohara 		n = btbc_write(sc, offset, rptr, m->m_len - rlen);
    650  1.1  kiyohara 		count += n;
    651  1.1  kiyohara 		rlen += n;
    652  1.1  kiyohara 		rptr += n;
    653  1.1  kiyohara 
    654  1.1  kiyohara 		/* Tell the FPGA to send the data */
    655  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    656  1.1  kiyohara 		    BLUECARD_COMMAND, command);
    657  1.1  kiyohara 
    658  1.1  kiyohara 		if (set_baudrate) {
    659  1.1  kiyohara 			unsigned char baud_reg;
    660  1.1  kiyohara 
    661  1.1  kiyohara 			switch (*(uint8_t *)(p + 1)) {
    662  1.1  kiyohara 			case 0x00:	/* baud rate 460800 */
    663  1.1  kiyohara 				baud_reg = BLUECARD_CONTROL_BAUDRATE_460800;
    664  1.1  kiyohara 				break;
    665  1.1  kiyohara 			case 0x01:	/* baud rate 230400 */
    666  1.1  kiyohara 				baud_reg = BLUECARD_CONTROL_BAUDRATE_230400;
    667  1.1  kiyohara 				break;
    668  1.1  kiyohara 			case 0x02:	/* baud rate 115200 */
    669  1.1  kiyohara 				baud_reg = BLUECARD_CONTROL_BAUDRATE_115200;
    670  1.1  kiyohara 				break;
    671  1.1  kiyohara 			case 0x03:	/* baud rate 57600 */
    672  1.1  kiyohara 			default:
    673  1.1  kiyohara 				baud_reg = BLUECARD_CONTROL_BAUDRATE_57600;
    674  1.1  kiyohara 				break;
    675  1.1  kiyohara 			}
    676  1.1  kiyohara 
    677  1.1  kiyohara 			/* Wait until the command reaches the baseband */
    678  1.1  kiyohara 			tsleep(sc, PCATCH, "btbc_wait", hz / 5);
    679  1.1  kiyohara 
    680  1.1  kiyohara 			/* Set baud on baseband */
    681  1.1  kiyohara 			sc->sc_ctrlreg &= ~BLUECARD_CONTROL_BAUDRATE_MASK;
    682  1.1  kiyohara 			sc->sc_ctrlreg |= baud_reg;
    683  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    684  1.1  kiyohara 			    BLUECARD_CONTROL, sc->sc_ctrlreg);
    685  1.1  kiyohara 
    686  1.1  kiyohara 			/* Enable RTS */
    687  1.1  kiyohara 			sc->sc_ctrlreg &= ~BLUECARD_CONTROL_RTS;
    688  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    689  1.1  kiyohara 			    BLUECARD_CONTROL, sc->sc_ctrlreg);
    690  1.1  kiyohara 
    691  1.1  kiyohara 			/* Wait before the next HCI packet can be send */
    692  1.1  kiyohara 			tsleep(sc, PCATCH, "btbc_wait", hz);
    693  1.1  kiyohara 
    694  1.1  kiyohara 			m_freem(m);
    695  1.1  kiyohara 			break;
    696  1.1  kiyohara 		}
    697  1.1  kiyohara 	}
    698  1.1  kiyohara 	sc->sc_unit.hci_stats.byte_tx += count;
    699  1.1  kiyohara }
    700  1.1  kiyohara 
    701  1.1  kiyohara static int
    702  1.1  kiyohara btbc_intr(void *arg)
    703  1.1  kiyohara {
    704  1.1  kiyohara 	struct btbc_softc *sc = arg;
    705  1.1  kiyohara 	int handled = 0;
    706  1.1  kiyohara 	uint8_t isr;
    707  1.1  kiyohara 
    708  1.1  kiyohara 	isr = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    709  1.1  kiyohara 	    BLUECARD_INTERRUPT);
    710  1.1  kiyohara 	if (isr != 0x00 && isr != 0xff) {
    711  1.1  kiyohara 		if (isr & BLUECARD_INTERRUPT_RXBUF1) {
    712  1.1  kiyohara 			isr &= ~BLUECARD_INTERRUPT_RXBUF1;
    713  1.1  kiyohara 			handled = 1;
    714  1.1  kiyohara 			btbc_receive(sc, BLUECARD_BUF1);
    715  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    716  1.1  kiyohara 			    BLUECARD_INTERRUPT, BLUECARD_INTERRUPT_RXBUF1);
    717  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    718  1.1  kiyohara 			    BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF1);
    719  1.1  kiyohara 		}
    720  1.1  kiyohara 		if (isr & BLUECARD_INTERRUPT_RXBUF2) {
    721  1.1  kiyohara 			isr &= ~BLUECARD_INTERRUPT_RXBUF2;
    722  1.1  kiyohara 			handled = 1;
    723  1.1  kiyohara 			btbc_receive(sc, BLUECARD_BUF2);
    724  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    725  1.1  kiyohara 			    BLUECARD_INTERRUPT, BLUECARD_INTERRUPT_RXBUF2);
    726  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    727  1.1  kiyohara 			    BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF2);
    728  1.1  kiyohara 		}
    729  1.1  kiyohara 		if (isr & BLUECARD_INTERRUPT_TXBUF1) {
    730  1.1  kiyohara 			isr &= ~BLUECARD_INTERRUPT_TXBUF1;
    731  1.1  kiyohara 			handled = 1;
    732  1.1  kiyohara 			sc->sc_txstate |= TXBUF1_EMPTY;
    733  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    734  1.1  kiyohara 			    BLUECARD_INTERRUPT, BLUECARD_INTERRUPT_TXBUF1);
    735  1.1  kiyohara 			btbc_transmit(sc);
    736  1.1  kiyohara 		}
    737  1.1  kiyohara 		if (isr & BLUECARD_INTERRUPT_TXBUF2) {
    738  1.1  kiyohara 			isr &= ~BLUECARD_INTERRUPT_TXBUF2;
    739  1.1  kiyohara 			handled = 1;
    740  1.1  kiyohara 			sc->sc_txstate |= TXBUF2_EMPTY;
    741  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    742  1.1  kiyohara 			    BLUECARD_INTERRUPT, BLUECARD_INTERRUPT_TXBUF2);
    743  1.1  kiyohara 			btbc_transmit(sc);
    744  1.1  kiyohara 		}
    745  1.1  kiyohara 
    746  1.1  kiyohara 		if (isr & 0x40) {	/* card eject ? */
    747  1.1  kiyohara 			printf("card eject?\n");
    748  1.1  kiyohara 			isr &= ~0x40;
    749  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    750  1.1  kiyohara 			    BLUECARD_INTERRUPT, 0x40);
    751  1.1  kiyohara 		}
    752  1.1  kiyohara 		if (isr != 0x00) {
    753  1.1  kiyohara 			printf("unknwon intrrupt: isr=0x%x\n", isr);
    754  1.1  kiyohara 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    755  1.1  kiyohara 			    BLUECARD_INTERRUPT, isr);
    756  1.1  kiyohara 		}
    757  1.1  kiyohara 	}
    758  1.1  kiyohara 
    759  1.1  kiyohara 	return handled;
    760  1.1  kiyohara }
    761  1.1  kiyohara 
    762  1.1  kiyohara /*
    763  1.1  kiyohara  * start sending on btbc
    764  1.1  kiyohara  * this should be called only when BTF_XMIT is not set, and
    765  1.1  kiyohara  * we only send cmd packets that are clear to send
    766  1.1  kiyohara  */
    767  1.1  kiyohara static void
    768  1.1  kiyohara btbc_start(struct hci_unit *unit)
    769  1.1  kiyohara {
    770  1.1  kiyohara 	struct btbc_softc *sc = unit->hci_softc;
    771  1.1  kiyohara 	struct mbuf *m;
    772  1.1  kiyohara 
    773  1.1  kiyohara 	KASSERT((unit->hci_flags & BTF_XMIT) == 0);
    774  1.1  kiyohara 	KASSERT(sc->sc_txp == NULL);
    775  1.1  kiyohara 
    776  1.1  kiyohara 	if (MBUFQ_FIRST(&unit->hci_cmdq)) {
    777  1.1  kiyohara 		MBUFQ_DEQUEUE(&unit->hci_cmdq, m);
    778  1.1  kiyohara 		unit->hci_stats.cmd_tx++;
    779  1.1  kiyohara 		M_SETCTX(m, NULL);
    780  1.1  kiyohara 		goto start;
    781  1.1  kiyohara 	}
    782  1.1  kiyohara 
    783  1.1  kiyohara 	if (MBUFQ_FIRST(&unit->hci_scotxq)) {
    784  1.1  kiyohara 		MBUFQ_DEQUEUE(&unit->hci_scotxq, m);
    785  1.1  kiyohara 		unit->hci_stats.sco_tx++;
    786  1.1  kiyohara 		goto start;
    787  1.1  kiyohara 	}
    788  1.1  kiyohara 
    789  1.1  kiyohara 	if (MBUFQ_FIRST(&unit->hci_acltxq)) {
    790  1.1  kiyohara 		MBUFQ_DEQUEUE(&unit->hci_acltxq, m);
    791  1.1  kiyohara 		unit->hci_stats.acl_tx++;
    792  1.1  kiyohara 		M_SETCTX(m, NULL);
    793  1.1  kiyohara 		goto start;
    794  1.1  kiyohara 	}
    795  1.1  kiyohara 
    796  1.1  kiyohara 	/* Nothing to send */
    797  1.1  kiyohara 	return;
    798  1.1  kiyohara 
    799  1.1  kiyohara start:
    800  1.1  kiyohara 	sc->sc_txp = m;
    801  1.1  kiyohara 	unit->hci_flags |= BTF_XMIT;
    802  1.1  kiyohara 	btbc_transmit(sc);
    803  1.1  kiyohara }
    804  1.1  kiyohara 
    805  1.1  kiyohara static int
    806  1.1  kiyohara btbc_enable(struct hci_unit *unit)
    807  1.1  kiyohara {
    808  1.1  kiyohara 	struct btbc_softc *sc = unit->hci_softc;
    809  1.1  kiyohara 	int err;
    810  1.1  kiyohara 	uint8_t id, ctrl;
    811  1.1  kiyohara 
    812  1.1  kiyohara 	if (unit->hci_flags & BTF_RUNNING)
    813  1.1  kiyohara 		return 0;
    814  1.1  kiyohara 
    815  1.1  kiyohara 	sc->sc_txstate = TXBUF1_EMPTY | TXBUF2_EMPTY;
    816  1.1  kiyohara 	sc->sc_intr = pcmcia_intr_establish(sc->sc_pf, IPL_TTY, btbc_intr, sc);
    817  1.1  kiyohara 	if (sc->sc_intr == NULL) {
    818  1.1  kiyohara 		err = EIO;
    819  1.1  kiyohara 		goto fail1;
    820  1.1  kiyohara 	}
    821  1.1  kiyohara 
    822  1.1  kiyohara 	err = pcmcia_function_enable(sc->sc_pf);
    823  1.1  kiyohara 	if (err)
    824  1.1  kiyohara 		goto fail2;
    825  1.1  kiyohara 
    826  1.1  kiyohara 	unit->hci_flags |= BTF_RUNNING;
    827  1.1  kiyohara 	unit->hci_flags &= ~BTF_XMIT;
    828  1.1  kiyohara 
    829  1.1  kiyohara 	/* Reset card */
    830  1.1  kiyohara 	ctrl = BLUECARD_CONTROL_RESET | BLUECARD_CONTROL_CARDRESET;
    831  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
    832  1.1  kiyohara 	    ctrl);
    833  1.1  kiyohara 
    834  1.1  kiyohara 	/* Turn FPGA off */
    835  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    836  1.1  kiyohara 	    BLUECARD_CARDRESET, 0x80);
    837  1.1  kiyohara 
    838  1.1  kiyohara 	/* Wait some time */
    839  1.1  kiyohara 	tsleep(sc, PCATCH, "btbc_reset", 1);
    840  1.1  kiyohara 
    841  1.1  kiyohara 	/* Turn FPGA on */
    842  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    843  1.1  kiyohara 	    BLUECARD_CARDRESET, 0x00);
    844  1.1  kiyohara 
    845  1.1  kiyohara 	/* Activate card */
    846  1.1  kiyohara 	ctrl = BLUECARD_CONTROL_ON | BLUECARD_CONTROL_RESPU;
    847  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
    848  1.1  kiyohara 	    ctrl);
    849  1.1  kiyohara 
    850  1.1  kiyohara 	tsleep(sc, PCATCH, "btbc_enable", 1);
    851  1.1  kiyohara 	sc->sc_ctrlreg = ctrl;
    852  1.1  kiyohara 
    853  1.1  kiyohara 	/* Enable interrupt */
    854  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    855  1.1  kiyohara 	    BLUECARD_INTERRUPT, 0xff);
    856  1.1  kiyohara 	sc->sc_ctrlreg |= BLUECARD_CONTROL_INTERRUPT;
    857  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
    858  1.1  kiyohara 	    sc->sc_ctrlreg);
    859  1.1  kiyohara 
    860  1.1  kiyohara 	id = bus_space_read_1(sc->sc_pcioh.iot,
    861  1.1  kiyohara 	    sc->sc_pcioh.ioh, BLUECARD_LEDCONTROL);
    862  1.1  kiyohara 	switch (id & 0x0f) {
    863  1.1  kiyohara 	case 0x02:
    864  1.1  kiyohara 		/* Enable LED */
    865  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    866  1.1  kiyohara 		    BLUECARD_LEDCONTROL, 0x08 | 0x20);
    867  1.1  kiyohara 		break;
    868  1.1  kiyohara 
    869  1.1  kiyohara 	case 0x03:
    870  1.1  kiyohara 		/* Disable RTS */
    871  1.1  kiyohara 		ctrl |= BLUECARD_CONTROL_RTS;
    872  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    873  1.1  kiyohara 		    BLUECARD_CONTROL, ctrl);
    874  1.1  kiyohara 
    875  1.1  kiyohara 		/* Set baud rate */
    876  1.1  kiyohara 		ctrl |= BLUECARD_CONTROL_BAUDRATE_460800;
    877  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    878  1.1  kiyohara 		    BLUECARD_CONTROL, ctrl);
    879  1.1  kiyohara 
    880  1.1  kiyohara 		/* Enable RTS */
    881  1.1  kiyohara 		ctrl &= ~BLUECARD_CONTROL_RTS;
    882  1.1  kiyohara 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    883  1.1  kiyohara 		    BLUECARD_CONTROL, ctrl);
    884  1.1  kiyohara 		break;
    885  1.1  kiyohara 	}
    886  1.1  kiyohara 
    887  1.1  kiyohara 	/* Start the RX buffers */
    888  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    889  1.1  kiyohara 	    BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF1);
    890  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    891  1.1  kiyohara 	    BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF2);
    892  1.1  kiyohara 
    893  1.1  kiyohara 	/* XXX: Control the point at which RTS is enabled */
    894  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    895  1.1  kiyohara 	    BLUECARD_RXCONTROL, BLUECARD_RXCONTROL_RTSLEVEL(0x0f) | 1);
    896  1.1  kiyohara 
    897  1.1  kiyohara 	/* Timeout before it is safe to send the first HCI packet */
    898  1.1  kiyohara 	tsleep(sc, PCATCH, "btbc_enable", hz * 2);
    899  1.1  kiyohara 
    900  1.1  kiyohara 	btbc_set_baudrate(sc, BTBC_DEFAULT_BAUDRATE);
    901  1.1  kiyohara 
    902  1.1  kiyohara 	return 0;
    903  1.1  kiyohara 
    904  1.1  kiyohara fail2:
    905  1.1  kiyohara 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
    906  1.1  kiyohara 	sc->sc_intr = NULL;
    907  1.1  kiyohara fail1:
    908  1.1  kiyohara 	return err;
    909  1.1  kiyohara }
    910  1.1  kiyohara 
    911  1.1  kiyohara static void
    912  1.1  kiyohara btbc_disable(struct hci_unit *unit)
    913  1.1  kiyohara {
    914  1.1  kiyohara 	struct btbc_softc *sc = unit->hci_softc;
    915  1.1  kiyohara 
    916  1.1  kiyohara 	if ((unit->hci_flags & BTF_RUNNING) == 0)
    917  1.1  kiyohara 		return;
    918  1.1  kiyohara 
    919  1.1  kiyohara 	pcmcia_function_disable(sc->sc_pf);
    920  1.1  kiyohara 
    921  1.1  kiyohara 	if (sc->sc_intr) {
    922  1.1  kiyohara 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
    923  1.1  kiyohara 		sc->sc_intr = NULL;
    924  1.1  kiyohara 	}
    925  1.1  kiyohara 
    926  1.1  kiyohara 	if (sc->sc_rxp) {
    927  1.1  kiyohara 		m_freem(sc->sc_rxp);
    928  1.1  kiyohara 		sc->sc_rxp = NULL;
    929  1.1  kiyohara 	}
    930  1.1  kiyohara 
    931  1.1  kiyohara 	if (sc->sc_txp) {
    932  1.1  kiyohara 		m_freem(sc->sc_txp);
    933  1.1  kiyohara 		sc->sc_txp = NULL;
    934  1.1  kiyohara 	}
    935  1.1  kiyohara 
    936  1.1  kiyohara 	unit->hci_flags &= ~BTF_RUNNING;
    937  1.1  kiyohara 
    938  1.1  kiyohara 	/* Disable LED */
    939  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    940  1.1  kiyohara 	    BLUECARD_LEDCONTROL, 0x00);
    941  1.1  kiyohara 
    942  1.1  kiyohara 	/* Reset card */
    943  1.1  kiyohara 	sc->sc_ctrlreg = BLUECARD_CONTROL_RESET | BLUECARD_CONTROL_CARDRESET;
    944  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
    945  1.1  kiyohara 	    sc->sc_ctrlreg);
    946  1.1  kiyohara 
    947  1.1  kiyohara 	/* Turn FPGA off */
    948  1.1  kiyohara 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    949  1.1  kiyohara 	    BLUECARD_CARDRESET, 0x80);
    950  1.1  kiyohara }
    951