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