Home | History | Annotate | Line # | Download | only in pcmcia
btbc.c revision 1.10
      1 /*	$NetBSD: btbc.c,v 1.10 2007/11/25 23:32:44 kiyohara 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.10 2007/11/25 23:32:44 kiyohara 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 #ifndef BTBC_DEFAULT_BAUDRATE
     70 #define BTBC_DEFAULT_BAUDRATE	57600
     71 #endif
     72 
     73 struct btbc_softc {
     74 	device_t sc_dev;
     75 
     76 	struct pcmcia_function *sc_pf;		/* our PCMCIA function */
     77 	struct pcmcia_io_handle sc_pcioh;	/* PCMCIA i/o space info */
     78 	void *sc_powerhook;			/* power hook descriptor */
     79 	int sc_flags;				/* flags */
     80 
     81 	struct hci_unit sc_unit;		/* Bluetooth HCI Unit */
     82 
     83 	/* hardware interrupt */
     84 	void *sc_intr;				/* cookie */
     85 	int sc_state;				/* receive state */
     86 	int sc_want;				/* how much we want */
     87 	struct mbuf *sc_rxp;			/* incoming packet */
     88 	struct mbuf *sc_txp;			/* outgoing packet */
     89 	int sc_txstate;
     90 #define TXBUF1_EMPTY	(1 << 0)
     91 #define TXBUF2_EMPTY	(1 << 1)
     92 #define TXBUF_MASK	(1 << 2)
     93 
     94 	callout_t sc_ledch;			/* callout handler for LED */
     95 	uint8_t sc_ctrlreg;			/* value for control register */
     96 };
     97 
     98 static int btbc_match(device_t, struct cfdata *, void *);
     99 static void btbc_attach(device_t, device_t, void *);
    100 static int btbc_detach(device_t, int);
    101 static void btbc_power(int, void *);
    102 
    103 static void btbc_activity_led_timeout(void *);
    104 static void btbc_enable_activity_led(struct btbc_softc *);
    105 static int btbc_read(struct btbc_softc *, uint32_t, uint8_t *, int);
    106 static int btbc_write(struct btbc_softc *, uint32_t, uint8_t *, int);
    107 static int btbc_set_baudrate(struct btbc_softc *, int);
    108 static void btbc_receive(struct btbc_softc *, uint32_t);
    109 static void btbc_transmit(struct btbc_softc *);
    110 static int btbc_intr(void *);
    111 
    112 static void btbc_start(device_t);
    113 static int btbc_enable(device_t);
    114 static void btbc_disable(device_t);
    115 
    116 CFATTACH_DECL_NEW(btbc, sizeof(struct btbc_softc),
    117     btbc_match, btbc_attach, btbc_detach, NULL);
    118 
    119 
    120 /* ARGSUSED */
    121 static int
    122 btbc_match(device_t parent, struct cfdata *match, void *aux)
    123 {
    124 	struct pcmcia_attach_args *pa = aux;
    125 
    126 	if (pa->manufacturer == PCMCIA_VENDOR_ANYCOM)
    127 		if ((pa->product == PCMCIA_PRODUCT_ANYCOM_LSE041) ||
    128 		    (pa->product == PCMCIA_PRODUCT_ANYCOM_LSE039) ||
    129 		    (pa->product == PCMCIA_PRODUCT_ANYCOM_LSE139))
    130 			return 1;
    131 	return 0;
    132 }
    133 
    134 static int
    135 btbc_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
    136 {
    137 
    138 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
    139 	    cfe->num_iospace < 1 || cfe->num_iospace > 2)
    140 		return EINVAL;
    141 	return 0;
    142 }
    143 
    144 /* ARGSUSED */
    145 static void
    146 btbc_attach(device_t parent, device_t self, void *aux)
    147 {
    148 	struct btbc_softc *sc = device_private(self);
    149 	struct pcmcia_attach_args *pa = aux;
    150 	struct pcmcia_config_entry *cfe;
    151 	int error;
    152 
    153 	sc->sc_dev = self;
    154 	sc->sc_pf = pa->pf;
    155 
    156 	if ((error = pcmcia_function_configure(pa->pf,
    157 	    btbc_pcmcia_validate_config)) != 0) {
    158 		aprint_error_dev(self, "configure failed, error=%d\n", error);
    159 		return;
    160 	}
    161 
    162 	cfe = pa->pf->cfe;
    163 	sc->sc_pcioh = cfe->iospace[0].handle;
    164 
    165 	/* Attach Bluetooth unit */
    166 	sc->sc_unit.hci_dev = self;
    167 	sc->sc_unit.hci_enable = btbc_enable;
    168 	sc->sc_unit.hci_disable = btbc_disable;
    169 	sc->sc_unit.hci_start_cmd = btbc_start;
    170 	sc->sc_unit.hci_start_acl = btbc_start;
    171 	sc->sc_unit.hci_start_sco = btbc_start;
    172 	sc->sc_unit.hci_ipl = makeiplcookie(IPL_TTY);
    173 	hci_attach(&sc->sc_unit);
    174 
    175 	/* establish a power change hook */
    176 	sc->sc_powerhook = powerhook_establish(device_xname(sc->sc_dev),
    177 	    btbc_power, sc);
    178 
    179 	callout_init(&sc->sc_ledch, 0);
    180 	callout_setfunc(&sc->sc_ledch, btbc_activity_led_timeout, sc);
    181 
    182 	return;
    183 }
    184 
    185 /* ARGSUSED */
    186 static int
    187 btbc_detach(device_t self, int flags)
    188 {
    189 	struct btbc_softc *sc = device_private(self);
    190 	int err = 0;
    191 
    192 	btbc_disable(sc->sc_dev);
    193 
    194 	if (sc->sc_powerhook) {
    195 		powerhook_disestablish(sc->sc_powerhook);
    196 		sc->sc_powerhook = NULL;
    197 	}
    198 
    199 	callout_stop(&sc->sc_ledch);
    200 	callout_destroy(&sc->sc_ledch);
    201 
    202 	hci_detach(&sc->sc_unit);
    203 
    204 	pcmcia_function_unconfigure(sc->sc_pf);
    205 
    206 	return err;
    207 }
    208 
    209 static void
    210 btbc_power(int why, void *arg)
    211 {
    212 	struct btbc_softc *sc = arg;
    213 
    214 	switch(why) {
    215 	case PWR_SUSPEND:
    216 	case PWR_STANDBY:
    217 		if (sc->sc_unit.hci_flags & BTF_RUNNING) {
    218 			hci_detach(&sc->sc_unit);
    219 
    220 			sc->sc_flags |= BTBC_SLEEPING;
    221 			aprint_verbose_dev(sc->sc_dev, "sleeping\n");
    222 		}
    223 		break;
    224 
    225 	case PWR_RESUME:
    226 		if (sc->sc_flags & BTBC_SLEEPING) {
    227 			aprint_verbose_dev(sc->sc_dev, "waking up\n");
    228 			sc->sc_flags &= ~BTBC_SLEEPING;
    229 
    230 			memset(&sc->sc_unit, 0, sizeof(sc->sc_unit));
    231 			sc->sc_unit.hci_dev = sc->sc_dev;
    232 			sc->sc_unit.hci_enable = btbc_enable;
    233 			sc->sc_unit.hci_disable = btbc_disable;
    234 			sc->sc_unit.hci_start_cmd = btbc_start;
    235 			sc->sc_unit.hci_start_acl = btbc_start;
    236 			sc->sc_unit.hci_start_sco = btbc_start;
    237 			sc->sc_unit.hci_ipl = makeiplcookie(IPL_TTY);
    238 			hci_attach(&sc->sc_unit);
    239 		}
    240 		break;
    241 
    242 	case PWR_SOFTSUSPEND:
    243 	case PWR_SOFTSTANDBY:
    244 	case PWR_SOFTRESUME:
    245 		break;
    246 	}
    247 }
    248 
    249 static void
    250 btbc_activity_led_timeout(void *arg)
    251 {
    252 	struct btbc_softc *sc = arg;
    253 	uint8_t id;
    254 
    255 	id = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    256 	    BLUECARD_LEDCONTROL);
    257 	if (id & 0x20)
    258 		/* Disable activity LED */
    259 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    260 		    BLUECARD_LEDCONTROL, 0x08 | 0x20);
    261 	else
    262 		/* Disable power LED */
    263 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    264 		    BLUECARD_LEDCONTROL, 0x00);
    265 }
    266 
    267 static void
    268 btbc_enable_activity_led(struct btbc_softc *sc)
    269 {
    270 	uint8_t id;
    271 
    272 	id = bus_space_read_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    273 	    BLUECARD_LEDCONTROL);
    274 	if (id & 0x20) {
    275 		/* Enable activity LED */
    276 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    277 		    BLUECARD_LEDCONTROL, 0x10 | 0x40);
    278 
    279 		/* Stop the LED after hz/4 */
    280 		callout_schedule(&sc->sc_ledch, hz / 4);
    281 	} else {
    282 		/* Enable power LED */
    283 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    284 		    BLUECARD_LEDCONTROL, 0x08 | 0x20);
    285 
    286 		/* Stop the LED after HZ/2 */
    287 		callout_schedule(&sc->sc_ledch, hz / 2);
    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(sc->sc_dev);
    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 					aprint_error_dev(sc->sc_dev,
    407 					    "out of memory\n");
    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 					aprint_error_dev(sc->sc_dev,
    423 					    "out of memory\n");
    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 				aprint_error_dev(sc->sc_dev,
    476 				    "Unknown packet type=%#x!\n", 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 				device_xname(sc->sc_dev), 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_dev);
    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 			aprint_normal_dev(sc->sc_dev, "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 			aprint_error_dev(sc->sc_dev,
    728 			    "unknown interrupt: isr=0x%x\n", isr);
    729 			bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    730 			    BLUECARD_INTERRUPT, isr);
    731 		}
    732 	}
    733 
    734 	return handled;
    735 }
    736 
    737 /*
    738  * start sending on btbc
    739  * this should be called only when BTF_XMIT is not set, and
    740  * we only send cmd packets that are clear to send
    741  */
    742 static void
    743 btbc_start(device_t self)
    744 {
    745 	struct btbc_softc *sc = device_private(self);
    746 	struct hci_unit *unit = &sc->sc_unit;
    747 	struct mbuf *m;
    748 
    749 	KASSERT((unit->hci_flags & BTF_XMIT) == 0);
    750 	KASSERT(sc->sc_txp == NULL);
    751 
    752 	if (MBUFQ_FIRST(&unit->hci_cmdq)) {
    753 		MBUFQ_DEQUEUE(&unit->hci_cmdq, m);
    754 		unit->hci_stats.cmd_tx++;
    755 		M_SETCTX(m, NULL);
    756 		goto start;
    757 	}
    758 
    759 	if (MBUFQ_FIRST(&unit->hci_scotxq)) {
    760 		MBUFQ_DEQUEUE(&unit->hci_scotxq, m);
    761 		unit->hci_stats.sco_tx++;
    762 		goto start;
    763 	}
    764 
    765 	if (MBUFQ_FIRST(&unit->hci_acltxq)) {
    766 		MBUFQ_DEQUEUE(&unit->hci_acltxq, m);
    767 		unit->hci_stats.acl_tx++;
    768 		M_SETCTX(m, NULL);
    769 		goto start;
    770 	}
    771 
    772 	/* Nothing to send */
    773 	return;
    774 
    775 start:
    776 	sc->sc_txp = m;
    777 	unit->hci_flags |= BTF_XMIT;
    778 	btbc_transmit(sc);
    779 }
    780 
    781 static int
    782 btbc_enable(device_t self)
    783 {
    784 	struct btbc_softc *sc = device_private(self);
    785 	struct hci_unit *unit = &sc->sc_unit;
    786 	int err;
    787 	uint8_t id, ctrl;
    788 
    789 	if (unit->hci_flags & BTF_RUNNING)
    790 		return 0;
    791 
    792 	sc->sc_txstate = TXBUF1_EMPTY | TXBUF2_EMPTY;
    793 	sc->sc_intr = pcmcia_intr_establish(sc->sc_pf, IPL_TTY, btbc_intr, sc);
    794 	if (sc->sc_intr == NULL) {
    795 		err = EIO;
    796 		goto fail1;
    797 	}
    798 
    799 	err = pcmcia_function_enable(sc->sc_pf);
    800 	if (err)
    801 		goto fail2;
    802 
    803 	unit->hci_flags |= BTF_RUNNING;
    804 	unit->hci_flags &= ~BTF_XMIT;
    805 
    806 	/* Reset card */
    807 	ctrl = BLUECARD_CONTROL_RESET | BLUECARD_CONTROL_CARDRESET;
    808 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
    809 	    ctrl);
    810 
    811 	/* Turn FPGA off */
    812 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    813 	    BLUECARD_CARDRESET, 0x80);
    814 
    815 	/* Wait some time */
    816 	tsleep(sc, PCATCH, "btbc_reset", 1);
    817 
    818 	/* Turn FPGA on */
    819 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    820 	    BLUECARD_CARDRESET, 0x00);
    821 
    822 	/* Activate card */
    823 	ctrl = BLUECARD_CONTROL_ON | BLUECARD_CONTROL_RESPU;
    824 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
    825 	    ctrl);
    826 
    827 	tsleep(sc, PCATCH, "btbc_enable", 1);
    828 	sc->sc_ctrlreg = ctrl;
    829 
    830 	/* Enable interrupt */
    831 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    832 	    BLUECARD_INTERRUPT, 0xff);
    833 	sc->sc_ctrlreg |= BLUECARD_CONTROL_INTERRUPT;
    834 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
    835 	    sc->sc_ctrlreg);
    836 
    837 	id = bus_space_read_1(sc->sc_pcioh.iot,
    838 	    sc->sc_pcioh.ioh, BLUECARD_LEDCONTROL);
    839 	switch (id & 0x0f) {
    840 	case 0x02:
    841 		/* Enable LED */
    842 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    843 		    BLUECARD_LEDCONTROL, 0x08 | 0x20);
    844 		break;
    845 
    846 	case 0x03:
    847 		/* Disable RTS */
    848 		ctrl |= BLUECARD_CONTROL_RTS;
    849 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    850 		    BLUECARD_CONTROL, ctrl);
    851 
    852 		/* Set baud rate */
    853 		ctrl |= BLUECARD_CONTROL_BAUDRATE_460800;
    854 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    855 		    BLUECARD_CONTROL, ctrl);
    856 
    857 		/* Enable RTS */
    858 		ctrl &= ~BLUECARD_CONTROL_RTS;
    859 		bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    860 		    BLUECARD_CONTROL, ctrl);
    861 		break;
    862 	}
    863 
    864 	/* Start the RX buffers */
    865 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    866 	    BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF1);
    867 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    868 	    BLUECARD_COMMAND, BLUECARD_COMMAND_RXBUF2);
    869 
    870 	/* XXX: Control the point at which RTS is enabled */
    871 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    872 	    BLUECARD_RXCONTROL, BLUECARD_RXCONTROL_RTSLEVEL(0x0f) | 1);
    873 
    874 	/* Timeout before it is safe to send the first HCI packet */
    875 	tsleep(sc, PCATCH, "btbc_enable", hz * 2);
    876 
    877 	btbc_set_baudrate(sc, BTBC_DEFAULT_BAUDRATE);
    878 
    879 	return 0;
    880 
    881 fail2:
    882 	pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
    883 	sc->sc_intr = NULL;
    884 fail1:
    885 	return err;
    886 }
    887 
    888 static void
    889 btbc_disable(device_t self)
    890 {
    891 	struct btbc_softc *sc = device_private(self);
    892 	struct hci_unit *unit = &sc->sc_unit;
    893 
    894 	if ((unit->hci_flags & BTF_RUNNING) == 0)
    895 		return;
    896 
    897 	pcmcia_function_disable(sc->sc_pf);
    898 
    899 	if (sc->sc_intr) {
    900 		pcmcia_intr_disestablish(sc->sc_pf, sc->sc_intr);
    901 		sc->sc_intr = NULL;
    902 	}
    903 
    904 	if (sc->sc_rxp) {
    905 		m_freem(sc->sc_rxp);
    906 		sc->sc_rxp = NULL;
    907 	}
    908 
    909 	if (sc->sc_txp) {
    910 		m_freem(sc->sc_txp);
    911 		sc->sc_txp = NULL;
    912 	}
    913 
    914 	unit->hci_flags &= ~BTF_RUNNING;
    915 
    916 	/* Disable LED */
    917 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    918 	    BLUECARD_LEDCONTROL, 0x00);
    919 
    920 	/* Reset card */
    921 	sc->sc_ctrlreg = BLUECARD_CONTROL_RESET | BLUECARD_CONTROL_CARDRESET;
    922 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh, BLUECARD_CONTROL,
    923 	    sc->sc_ctrlreg);
    924 
    925 	/* Turn FPGA off */
    926 	bus_space_write_1(sc->sc_pcioh.iot, sc->sc_pcioh.ioh,
    927 	    BLUECARD_CARDRESET, 0x80);
    928 }
    929