Home | History | Annotate | Line # | Download | only in bluetooth
btsco.c revision 1.2
      1 /*	$NetBSD: btsco.c,v 1.2 2006/08/27 11:41:58 plunky Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2006 Itronix Inc.
      5  * All rights reserved.
      6  *
      7  * Written by Iain Hibbert for Itronix Inc.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of Itronix Inc. may not be used to endorse
     18  *    or promote products derived from this software without specific
     19  *    prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: btsco.c,v 1.2 2006/08/27 11:41:58 plunky Exp $");
     36 
     37 #include <sys/param.h>
     38 #include <sys/audioio.h>
     39 #include <sys/conf.h>
     40 #include <sys/device.h>
     41 #include <sys/fcntl.h>
     42 #include <sys/kernel.h>
     43 #include <sys/queue.h>
     44 #include <sys/malloc.h>
     45 #include <sys/mbuf.h>
     46 #include <sys/proc.h>
     47 #include <sys/systm.h>
     48 
     49 #include <prop/proplib.h>
     50 
     51 #include <netbt/bluetooth.h>
     52 #include <netbt/rfcomm.h>
     53 #include <netbt/sco.h>
     54 
     55 #include <dev/audio_if.h>
     56 #include <dev/auconv.h>
     57 #include <dev/mulaw.h>
     58 
     59 #include <dev/bluetooth/btdev.h>
     60 #include <dev/bluetooth/btsco.h>
     61 
     62 #undef DPRINTF
     63 #undef DPRINTFN
     64 
     65 #ifdef BTSCO_DEBUG
     66 int btsco_debug = BTSCO_DEBUG;
     67 #define DPRINTF(fmt, args...)		do {		\
     68 	if (btsco_debug)				\
     69 		printf("%s: "fmt, __func__ , ##args);	\
     70 } while (/* CONSTCOND */0)
     71 
     72 #define DPRINTFN(n, fmt, args...)	do {		\
     73 	if (btsco_debug > (n))				\
     74 		printf("%s: "fmt, __func__ , ##args);	\
     75 } while (/* CONSTCOND */0)
     76 #else
     77 #define DPRINTF(...)
     78 #define DPRINTFN(...)
     79 #endif
     80 
     81 /*****************************************************************************
     82  *
     83  *	Bluetooth SCO Audio device
     84  */
     85 
     86 /* btsco softc */
     87 struct btsco_softc {
     88 	struct device		 sc_dev;
     89 	uint16_t		 sc_flags;
     90 
     91 	struct device		*sc_audio;	/* MI audio device */
     92 	void			*sc_intr;	/* interrupt cookie */
     93 
     94 	/* Bluetooth */
     95 	bdaddr_t		 sc_laddr;	/* local address */
     96 	bdaddr_t		 sc_raddr;	/* remote address */
     97 	uint16_t		 sc_state;	/* link state */
     98 	struct sco_pcb		*sc_sco;	/* SCO handle */
     99 	struct sco_pcb		*sc_sco_l;	/* SCO listen handle */
    100 	int			 sc_mtu;	/* SCO mtu */
    101 	uint8_t			 sc_channel;	/* RFCOMM channel */
    102 	int			 sc_err;	/* stored error */
    103 
    104 	/* Receive */
    105 	int			 sc_rx_want;	/* bytes wanted */
    106 	uint8_t			*sc_rx_block;	/* receive block */
    107 	void		       (*sc_rx_intr)(void *);	/* callback */
    108 	void			*sc_rx_intrarg;	/* callback arg */
    109 	struct mbuf		*sc_rx_mbuf;	/* leftover mbuf */
    110 
    111 	/* Transmit */
    112 	int			 sc_tx_size;	/* bytes to send */
    113 	int			 sc_tx_pending;	/* packets pending */
    114 	uint8_t			*sc_tx_block;	/* transmit block */
    115 	void		       (*sc_tx_intr)(void *);	/* callback */
    116 	void			*sc_tx_intrarg;	/* callback arg */
    117 	void			*sc_tx_buf;	/* transmit buffer */
    118 	int			 sc_tx_refcnt;	/* buffer refcnt */
    119 
    120 	/* mixer data */
    121 	int			 sc_vgs;	/* speaker volume */
    122 	int			 sc_vgm;	/* mic volume */
    123 };
    124 
    125 /* sc_state */
    126 #define BTSCO_CLOSED		0
    127 #define BTSCO_WAIT_CONNECT	1
    128 #define BTSCO_OPEN		2
    129 
    130 /* sc_flags */
    131 #define BTSCO_LISTEN		(1 << 1)
    132 
    133 /* autoconf(9) glue */
    134 static int  btsco_match(struct device *, struct cfdata *, void *);
    135 static void btsco_attach(struct device *, struct device *, void *);
    136 static int  btsco_detach(struct device *, int);
    137 
    138 CFATTACH_DECL(btsco, sizeof(struct btsco_softc),
    139     btsco_match, btsco_attach, btsco_detach, NULL);
    140 
    141 /* audio(9) glue */
    142 static int btsco_open(void *, int);
    143 static void btsco_close(void *);
    144 static int btsco_query_encoding(void *, struct audio_encoding *);
    145 static int btsco_set_params(void *, int, int, audio_params_t *, audio_params_t *,
    146 				stream_filter_list_t *, stream_filter_list_t *);
    147 static int btsco_round_blocksize(void *, int, int, const audio_params_t *);
    148 static int btsco_start_output(void *, void *, int, void (*)(void *), void *);
    149 static int btsco_start_input(void *, void *, int, void (*)(void *), void *);
    150 static int btsco_halt_output(void *);
    151 static int btsco_halt_input(void *);
    152 static int btsco_getdev(void *, struct audio_device *);
    153 static int btsco_setfd(void *, int);
    154 static int btsco_set_port(void *, mixer_ctrl_t *);
    155 static int btsco_get_port(void *, mixer_ctrl_t *);
    156 static int btsco_query_devinfo(void *, mixer_devinfo_t *);
    157 static void *btsco_allocm(void *, int, size_t, struct malloc_type *, int);
    158 static void btsco_freem(void *, void *, struct malloc_type *);
    159 static int btsco_get_props(void *);
    160 static int btsco_dev_ioctl(void *, u_long, caddr_t, int, struct lwp *);
    161 
    162 static const struct audio_hw_if btsco_if = {
    163 	btsco_open,		/* open */
    164 	btsco_close,		/* close */
    165 	NULL,			/* drain */
    166 	btsco_query_encoding,	/* query_encoding */
    167 	btsco_set_params,	/* set_params */
    168 	btsco_round_blocksize,	/* round_blocksize */
    169 	NULL,			/* commit_settings */
    170 	NULL,			/* init_output */
    171 	NULL,			/* init_input */
    172 	btsco_start_output,	/* start_output */
    173 	btsco_start_input,	/* start_input */
    174 	btsco_halt_output,	/* halt_output */
    175 	btsco_halt_input,	/* halt_input */
    176 	NULL,			/* speaker_ctl */
    177 	btsco_getdev,		/* getdev */
    178 	btsco_setfd,		/* setfd */
    179 	btsco_set_port,	/* set_port */
    180 	btsco_get_port,	/* get_port */
    181 	btsco_query_devinfo,	/* query_devinfo */
    182 	btsco_allocm,		/* allocm */
    183 	btsco_freem,		/* freem */
    184 	NULL,			/* round_buffersize */
    185 	NULL,			/* mappage */
    186 	btsco_get_props,	/* get_props */
    187 	NULL,			/* trigger_output */
    188 	NULL,			/* trigger_input */
    189 	btsco_dev_ioctl,	/* dev_ioctl */
    190 	NULL,			/* powerstate */
    191 };
    192 
    193 static const struct audio_device btsco_device = {
    194 	"Bluetooth Audio",
    195 	"",
    196 	"btsco"
    197 };
    198 
    199 /* bluetooth(9) glue for SCO */
    200 static void  btsco_sco_connecting(void *);
    201 static void  btsco_sco_connected(void *);
    202 static void  btsco_sco_disconnected(void *, int);
    203 static void *btsco_sco_newconn(void *, struct sockaddr_bt *, struct sockaddr_bt *);
    204 static void  btsco_sco_complete(void *, int);
    205 static void  btsco_sco_input(void *, struct mbuf *);
    206 
    207 static const struct btproto btsco_sco_proto = {
    208 	btsco_sco_connecting,
    209 	btsco_sco_connected,
    210 	btsco_sco_disconnected,
    211 	btsco_sco_newconn,
    212 	btsco_sco_complete,
    213 	btsco_sco_input,
    214 };
    215 
    216 
    217 /*****************************************************************************
    218  *
    219  *	btsco definitions
    220  */
    221 
    222 /*
    223  * btsco mixer class
    224  */
    225 #define BTSCO_VGS		0
    226 #define BTSCO_VGM		1
    227 #define BTSCO_INPUT_CLASS	2
    228 #define BTSCO_OUTPUT_CLASS	3
    229 
    230 /* connect timeout */
    231 #define BTSCO_TIMEOUT		(30 * hz)
    232 
    233 /* misc btsco functions */
    234 static void btsco_extfree(struct mbuf *, caddr_t, size_t, void *);
    235 static void btsco_intr(void *);
    236 
    237 
    238 /*****************************************************************************
    239  *
    240  *	btsco autoconf(9) routines
    241  */
    242 
    243 static int
    244 btsco_match(struct device *self, struct cfdata *cfdata, void *aux)
    245 {
    246 	prop_dictionary_t dict = aux;
    247 	prop_object_t obj;
    248 
    249 	obj = prop_dictionary_get(dict, "device-type");
    250 	return prop_string_equals_cstring(obj, "btsco");
    251 }
    252 
    253 static void
    254 btsco_attach(struct device *parent, struct device *self, void *aux)
    255 {
    256 	struct btsco_softc *sc = (struct btsco_softc *)self;
    257 	prop_dictionary_t dict = aux;
    258 	prop_object_t obj;
    259 
    260 	/*
    261 	 * Init softc
    262 	 */
    263 	sc->sc_vgs = 200;
    264 	sc->sc_vgm = 200;
    265 	sc->sc_state = BTSCO_CLOSED;
    266 
    267 	/*
    268 	 * copy in our configuration info
    269 	 */
    270 	obj = prop_dictionary_get(dict, "local-bdaddr");
    271 	bdaddr_copy(&sc->sc_laddr, prop_data_data_nocopy(obj));
    272 
    273 	obj = prop_dictionary_get(dict, "remote-bdaddr");
    274 	bdaddr_copy(&sc->sc_raddr, prop_data_data_nocopy(obj));
    275 
    276 	obj = prop_dictionary_get(dict, "listen");
    277 	if (obj && prop_object_type(obj) == PROP_TYPE_BOOL
    278 	    && prop_bool_true(obj)) {
    279 		sc->sc_flags |= BTSCO_LISTEN;
    280 		aprint_verbose(" listen mode");
    281 	}
    282 
    283 	obj = prop_dictionary_get(dict, "rfcomm-channel");
    284 	if (obj == NULL
    285 	    || prop_object_type(obj) != PROP_TYPE_NUMBER
    286 	    || prop_number_integer_value(obj) < RFCOMM_CHANNEL_MIN
    287 	    || prop_number_integer_value(obj) > RFCOMM_CHANNEL_MAX) {
    288 		aprint_error(" invalid rfcomm-channel");
    289 		return;
    290 	}
    291 	sc->sc_channel = prop_number_integer_value(obj);
    292 
    293 	aprint_verbose(" channel %d", sc->sc_channel);
    294 	aprint_normal("\n");
    295 
    296 	DPRINTF("sc=%p\n", sc);
    297 
    298 	/*
    299 	 * set up transmit interrupt
    300 	 */
    301 	sc->sc_intr = softintr_establish(IPL_SOFTNET, btsco_intr, sc);
    302 	if (sc->sc_intr == NULL) {
    303 		aprint_error("%s: softintr_establish failed\n", sc->sc_dev.dv_xname);
    304 		return;
    305 	}
    306 
    307 	/*
    308 	 * attach audio device
    309 	 */
    310 	sc->sc_audio = audio_attach_mi(&btsco_if, sc, &sc->sc_dev);
    311 	if (sc->sc_audio == NULL) {
    312 		aprint_error("%s: audio_attach_mi failed\n", sc->sc_dev.dv_xname);
    313 		return;
    314 	}
    315 }
    316 
    317 static int
    318 btsco_detach(struct device *self, int flags)
    319 {
    320 	struct btsco_softc *sc = (struct btsco_softc *)self;
    321 	int s;
    322 
    323 	DPRINTF("sc=%p\n", sc);
    324 
    325 	if (sc->sc_sco != NULL) {
    326 		DPRINTF("sc_sco=%p\n", sc->sc_sco);
    327 		s = splsoftnet();
    328 		sco_disconnect(sc->sc_sco, 0);
    329 		sco_detach(&sc->sc_sco);
    330 		sc->sc_sco = NULL;
    331 		splx(s);
    332 	}
    333 
    334 	if (sc->sc_sco_l != NULL) {
    335 		DPRINTF("sc_sco_l=%p\n", sc->sc_sco_l);
    336 		s = splsoftnet();
    337 		sco_detach(&sc->sc_sco_l);
    338 		sc->sc_sco_l = NULL;
    339 		splx(s);
    340 	}
    341 
    342 	if (sc->sc_audio != NULL) {
    343 		DPRINTF("sc_audio=%p\n", sc->sc_audio);
    344 		config_detach(sc->sc_audio, flags);
    345 		sc->sc_audio = NULL;
    346 	}
    347 
    348 	if (sc->sc_intr != NULL) {
    349 		softintr_disestablish(sc->sc_intr);
    350 		sc->sc_intr = NULL;
    351 	}
    352 
    353 	if (sc->sc_rx_mbuf != NULL) {
    354 		m_freem(sc->sc_rx_mbuf);
    355 		sc->sc_rx_mbuf = NULL;
    356 	}
    357 
    358 	if (sc->sc_tx_refcnt > 0) {
    359 		printf("%s: tx_refcnt=%d!\n",
    360 			sc->sc_dev.dv_xname, sc->sc_tx_refcnt);
    361 
    362 		if ((flags & DETACH_FORCE) == 0)
    363 			return EAGAIN;
    364 	}
    365 
    366 	return 0;
    367 }
    368 
    369 
    370 /*****************************************************************************
    371  *
    372  *	bluetooth(9) methods for SCO
    373  *
    374  *	All these are called from Bluetooth Protocol code, in a soft
    375  *	interrupt context at IPL_SOFTNET.
    376  */
    377 
    378 static void
    379 btsco_sco_connecting(void *arg)
    380 {
    381 /*	struct btsco_softc *sc = arg;	*/
    382 
    383 	/* dont care */
    384 }
    385 
    386 static void
    387 btsco_sco_connected(void *arg)
    388 {
    389 	struct btsco_softc *sc = arg;
    390 
    391 	DPRINTF("%s\n", sc->sc_dev.dv_xname);
    392 
    393 	KASSERT(sc->sc_sco != NULL);
    394 	KASSERT(sc->sc_state == BTSCO_WAIT_CONNECT);
    395 
    396 	/*
    397 	 * If we are listening, no more need
    398 	 */
    399 	if (sc->sc_sco_l != NULL)
    400 		sco_detach(&sc->sc_sco_l);
    401 
    402 	sc->sc_state = BTSCO_OPEN;
    403 	wakeup(sc);
    404 }
    405 
    406 static void
    407 btsco_sco_disconnected(void *arg, int err)
    408 {
    409 	struct btsco_softc *sc = arg;
    410 	int s;
    411 
    412 	DPRINTF("%s sc_state %d\n", sc->sc_dev.dv_xname, sc->sc_state);
    413 
    414 	KASSERT(sc->sc_sco != NULL);
    415 
    416 	sc->sc_err = err;
    417 	sco_detach(&sc->sc_sco);
    418 
    419 	switch (sc->sc_state) {
    420 	case BTSCO_CLOSED:		/* dont think this can happen */
    421 		break;
    422 
    423 	case BTSCO_WAIT_CONNECT:	/* connect failed */
    424 		wakeup(sc);
    425 		break;
    426 
    427 	case BTSCO_OPEN:		/* link lost */
    428 		/*
    429 		 * If IO is in progress, tell the audio driver that it
    430 		 * has completed so that when it tries to send more, we
    431 		 * can indicate an error.
    432 		 */
    433 		s = splaudio();
    434 		if (sc->sc_tx_pending > 0) {
    435 			sc->sc_tx_pending = 0;
    436 			(*sc->sc_tx_intr)(sc->sc_tx_intrarg);
    437 		}
    438 		if (sc->sc_rx_want > 0) {
    439 			sc->sc_rx_want = 0;
    440 			(*sc->sc_rx_intr)(sc->sc_rx_intrarg);
    441 		}
    442 		splx(s);
    443 		break;
    444 
    445 	default:
    446 		UNKNOWN(sc->sc_state);
    447 	}
    448 
    449 	sc->sc_state = BTSCO_CLOSED;
    450 }
    451 
    452 static void *
    453 btsco_sco_newconn(void *arg, struct sockaddr_bt *laddr, struct sockaddr_bt *raddr)
    454 {
    455 	struct btsco_softc *sc = arg;
    456 
    457 	DPRINTF("%s\n", sc->sc_dev.dv_xname);
    458 	if (bdaddr_same(&raddr->bt_bdaddr, &sc->sc_raddr) == 0
    459 	    || sc->sc_state != BTSCO_WAIT_CONNECT
    460 	    || sc->sc_sco != NULL)
    461 	    return NULL;
    462 
    463 	sco_attach(&sc->sc_sco, &btsco_sco_proto, sc);
    464 	return sc->sc_sco;
    465 }
    466 
    467 static void
    468 btsco_sco_complete(void *arg, int count)
    469 {
    470 	struct btsco_softc *sc = arg;
    471 	int s;
    472 
    473 	DPRINTFN(10, "%s count %d\n", sc->sc_dev.dv_xname, count);
    474 
    475 	s = splaudio();
    476 	if (sc->sc_tx_pending > 0) {
    477 		sc->sc_tx_pending -= count;
    478 		if (sc->sc_tx_pending == 0)
    479 			(*sc->sc_tx_intr)(sc->sc_tx_intrarg);
    480 	}
    481 	splx(s);
    482 }
    483 
    484 static void
    485 btsco_sco_input(void *arg, struct mbuf *m)
    486 {
    487 	struct btsco_softc *sc = arg;
    488 	int len, s;
    489 
    490 	DPRINTFN(10, "%s len=%d\n", sc->sc_dev.dv_xname, m->m_pkthdr.len);
    491 
    492 	s = splaudio();
    493 	if (sc->sc_rx_want == 0) {
    494 		m_freem(m);
    495 	} else {
    496 		KASSERT(sc->sc_rx_intr != NULL);
    497 		KASSERT(sc->sc_rx_block != NULL);
    498 
    499 		len = MIN(sc->sc_rx_want, m->m_pkthdr.len);
    500 		m_copydata(m, 0, len, sc->sc_rx_block);
    501 
    502 		sc->sc_rx_want -= len;
    503 		sc->sc_rx_block += len;
    504 
    505 		if (len > m->m_pkthdr.len) {
    506 			if (sc->sc_rx_mbuf != NULL)
    507 				m_freem(sc->sc_rx_mbuf);
    508 
    509 			m_adj(m, len);
    510 			sc->sc_rx_mbuf = m;
    511 		} else {
    512 			m_freem(m);
    513 		}
    514 
    515 		if (sc->sc_rx_want == 0)
    516 			(*sc->sc_rx_intr)(sc->sc_rx_intrarg);
    517 	}
    518 	splx(s);
    519 }
    520 
    521 
    522 /*****************************************************************************
    523  *
    524  *	audio(9) methods
    525  *
    526  */
    527 
    528 static int
    529 btsco_open(void *hdl, int flags)
    530 {
    531 	struct sockaddr_bt sa;
    532 	struct btsco_softc *sc = hdl;
    533 	int err, s, timo;
    534 
    535 	DPRINTF("%s flags 0x%x\n", sc->sc_dev.dv_xname, flags);
    536 	/* flags FREAD & FWRITE? */
    537 
    538 	if (sc->sc_sco != NULL || sc->sc_sco_l != NULL)
    539 		return EIO;
    540 
    541 	s = splsoftnet();
    542 
    543 	memset(&sa, 0, sizeof(sa));
    544 	sa.bt_len = sizeof(sa);
    545 	sa.bt_family = AF_BLUETOOTH;
    546 	bdaddr_copy(&sa.bt_bdaddr, &sc->sc_laddr);
    547 
    548 	if (sc->sc_flags & BTSCO_LISTEN) {
    549 		err = sco_attach(&sc->sc_sco_l, &btsco_sco_proto, sc);
    550 		if (err)
    551 			goto done;
    552 
    553 		err = sco_bind(sc->sc_sco_l, &sa);
    554 		if (err) {
    555 			sco_detach(&sc->sc_sco_l);
    556 			goto done;
    557 		}
    558 
    559 		err = sco_listen(sc->sc_sco_l);
    560 		if (err) {
    561 			sco_detach(&sc->sc_sco_l);
    562 			goto done;
    563 		}
    564 
    565 		timo = 0;	/* no timeout */
    566 	} else {
    567 		err = sco_attach(&sc->sc_sco, &btsco_sco_proto, sc);
    568 		if (err)
    569 			goto done;
    570 
    571 		err = sco_bind(sc->sc_sco, &sa);
    572 		if (err) {
    573 			sco_detach(&sc->sc_sco);
    574 			goto done;
    575 		}
    576 
    577 		bdaddr_copy(&sa.bt_bdaddr, &sc->sc_raddr);
    578 		err = sco_connect(sc->sc_sco, &sa);
    579 		if (err) {
    580 			sco_detach(&sc->sc_sco);
    581 			goto done;
    582 		}
    583 
    584 		timo = BTSCO_TIMEOUT;
    585 	}
    586 
    587 	sc->sc_state = BTSCO_WAIT_CONNECT;
    588 	while (err == 0 && sc->sc_state == BTSCO_WAIT_CONNECT)
    589 		err = tsleep(sc, PWAIT | PCATCH, "btsco", timo);
    590 
    591 	switch (sc->sc_state) {
    592 	case BTSCO_CLOSED:		/* disconnected */
    593 		err = sc->sc_err;
    594 
    595 		/* fall through to */
    596 	case BTSCO_WAIT_CONNECT:	/* error */
    597 		if (sc->sc_sco != NULL)
    598 			sco_detach(&sc->sc_sco);
    599 
    600 		if (sc->sc_sco_l != NULL)
    601 			sco_detach(&sc->sc_sco_l);
    602 
    603 		break;
    604 
    605 	case BTSCO_OPEN:		/* hurrah */
    606 		sco_getopt(sc->sc_sco, SO_SCO_MTU, &sc->sc_mtu);
    607 		break;
    608 
    609 	default:
    610 		UNKNOWN(sc->sc_state);
    611 		break;
    612 	}
    613 
    614 done:
    615 	splx(s);
    616 
    617 	DPRINTF("done err=%d, sc_state=%d, sc_mtu=%d\n",
    618 			err, sc->sc_state, sc->sc_mtu);
    619 	return err;
    620 }
    621 
    622 static void
    623 btsco_close(void *hdl)
    624 {
    625 	struct btsco_softc *sc = hdl;
    626 	int s;
    627 
    628 	DPRINTF("%s\n", sc->sc_dev.dv_xname);
    629 
    630 	if (sc->sc_sco != NULL) {
    631 		s = splsoftnet();
    632 		sco_disconnect(sc->sc_sco, 0);
    633 		sco_detach(&sc->sc_sco);
    634 		splx(s);
    635 	}
    636 
    637 	if (sc->sc_sco_l != NULL) {
    638 		s = splsoftnet();
    639 		sco_detach(&sc->sc_sco_l);
    640 		splx(s);
    641 	}
    642 
    643 	if (sc->sc_rx_mbuf != NULL) {
    644 		m_freem(sc->sc_rx_mbuf);
    645 		sc->sc_rx_mbuf = NULL;
    646 	}
    647 
    648 	sc->sc_rx_want = 0;
    649 	sc->sc_rx_block = NULL;
    650 	sc->sc_rx_intr = NULL;
    651 	sc->sc_rx_intrarg = NULL;
    652 
    653 	sc->sc_tx_size = 0;
    654 	sc->sc_tx_block = NULL;
    655 	sc->sc_tx_pending = 0;
    656 	sc->sc_tx_intr = NULL;
    657 	sc->sc_tx_intrarg = NULL;
    658 }
    659 
    660 static int
    661 btsco_query_encoding(void *hdl, struct audio_encoding *ae)
    662 {
    663 /*	struct btsco_softc *sc = hdl;	*/
    664 	int err = 0;
    665 
    666 	switch (ae->index) {
    667 	case 0:
    668 		strcpy(ae->name, AudioEslinear_le);
    669 		ae->encoding = AUDIO_ENCODING_SLINEAR_LE;
    670 		ae->precision = 16;
    671 		ae->flags = 0;
    672 		break;
    673 
    674 	default:
    675 		err = EINVAL;
    676 	}
    677 
    678 	return err;
    679 }
    680 
    681 static int
    682 btsco_set_params(void *hdl, int setmode, int usemode,
    683 		audio_params_t *play, audio_params_t *rec,
    684 		stream_filter_list_t *pfil, stream_filter_list_t *rfil)
    685 {
    686 /*	struct btsco_softc *sc = hdl;	*/
    687 	audio_params_t hw;
    688 	int err = 0;
    689 
    690 	DPRINTF("setmode 0x%x usemode 0x%x\n", setmode, usemode);
    691 	DPRINTF("rate %d, precision %d, channels %d encoding %d\n",
    692 		play->sample_rate, play->precision, play->channels, play->encoding);
    693 
    694 	if ((play->precision != 16 && play->precision != 8)
    695 	    || play->sample_rate < 7500
    696 	    || play->sample_rate > 8500
    697 	    || play->channels != 1)
    698 		return EINVAL;
    699 
    700 	play->sample_rate = 8000;
    701 	hw = *play;
    702 
    703 	switch (play->encoding) {
    704 	case AUDIO_ENCODING_ULAW:
    705 		hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
    706 		pfil->append(pfil, mulaw_to_linear16, &hw);
    707 		break;
    708 
    709 	case AUDIO_ENCODING_ALAW:
    710 		hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
    711 		pfil->append(pfil, alaw_to_linear16, &hw);
    712 		break;
    713 
    714 	case AUDIO_ENCODING_SLINEAR_LE:
    715 		break;
    716 
    717 	case AUDIO_ENCODING_SLINEAR_BE:
    718 		if (play->precision == 16) {
    719 			hw.encoding = AUDIO_ENCODING_SLINEAR_LE;
    720 			pfil->append(pfil, swap_bytes, &hw);
    721 		}
    722 		break;
    723 
    724 	case AUDIO_ENCODING_ULINEAR_LE:
    725 	case AUDIO_ENCODING_ULINEAR_BE:
    726 	default:
    727 		err = EINVAL;
    728 	}
    729 
    730 	return err;
    731 }
    732 
    733 /*
    734  * If we have an MTU value to use, round the blocksize to that.
    735  */
    736 static int
    737 btsco_round_blocksize(void *hdl, int bs, int mode, const audio_params_t *param)
    738 {
    739 	struct btsco_softc *sc = hdl;
    740 
    741 	if (sc->sc_mtu > 0)
    742 		bs = (bs / sc->sc_mtu) * sc->sc_mtu;
    743 
    744 	DPRINTF("%s mode=0x%x, bs=%d, sc_mtu=%d\n",
    745 			sc->sc_dev.dv_xname, mode, bs, sc->sc_mtu);
    746 
    747 	return bs;
    748 }
    749 
    750 /*
    751  * Start Output
    752  *
    753  * We dont want to be calling the network stack at splaudio() so make
    754  * a note of what is to be sent, and schedule an interrupt to bundle
    755  * it up and queue it.
    756  */
    757 static int
    758 btsco_start_output(void *hdl, void *block, int blksize,
    759 		void (*intr)(void *), void *intrarg)
    760 {
    761 	struct btsco_softc *sc = hdl;
    762 
    763 	DPRINTFN(5, "%s blksize %d\n", sc->sc_dev.dv_xname, blksize);
    764 
    765 	if (sc->sc_sco == NULL)
    766 		return ENOTCONN;	/* connection lost */
    767 
    768 	sc->sc_tx_block = block;
    769 	sc->sc_tx_pending = 0;
    770 	sc->sc_tx_size = blksize;
    771 	sc->sc_tx_intr = intr;
    772 	sc->sc_tx_intrarg = intrarg;
    773 
    774 	softintr_schedule(sc->sc_intr);
    775 	return 0;
    776 }
    777 
    778 /*
    779  * Start Input
    780  *
    781  * When the SCO link is up, we are getting data in any case, so all we do
    782  * is note what we want and where to put it and let the sco_input routine
    783  * fill in the data.
    784  *
    785  * If there was any leftover data that didnt fit in the last block, retry
    786  * it now.
    787  */
    788 static int
    789 btsco_start_input(void *hdl, void *block, int blksize,
    790 		void (*intr)(void *), void *intrarg)
    791 {
    792 	struct btsco_softc *sc = hdl;
    793 	struct mbuf *m;
    794 
    795 	DPRINTFN(5, "%s blksize %d\n", sc->sc_dev.dv_xname, blksize);
    796 
    797 	if (sc->sc_sco == NULL)
    798 		return EINVAL;
    799 
    800 	sc->sc_rx_want = blksize;
    801 	sc->sc_rx_block = block;
    802 	sc->sc_rx_intr = intr;
    803 	sc->sc_rx_intrarg = intrarg;
    804 
    805 	if (sc->sc_rx_mbuf != NULL) {
    806 		m = sc->sc_rx_mbuf;
    807 		sc->sc_rx_mbuf = NULL;
    808 		btsco_sco_input(sc, m);
    809 	}
    810 
    811 	return 0;
    812 }
    813 
    814 /*
    815  * Halt Output
    816  *
    817  * This doesnt really halt the output, but it will look
    818  * that way to the audio driver. The current block will
    819  * still be transmitted.
    820  */
    821 static int
    822 btsco_halt_output(void *hdl)
    823 {
    824 	struct btsco_softc *sc = hdl;
    825 
    826 	DPRINTFN(5, "%s\n", sc->sc_dev.dv_xname);
    827 
    828 	sc->sc_tx_size = 0;
    829 	sc->sc_tx_block = NULL;
    830 	sc->sc_tx_pending = 0;
    831 	sc->sc_tx_intr = NULL;
    832 	sc->sc_tx_intrarg = NULL;
    833 
    834 	return 0;
    835 }
    836 
    837 /*
    838  * Halt Input
    839  *
    840  * This doesnt really halt the input, but it will look
    841  * that way to the audio driver. Incoming data will be
    842  * discarded.
    843  */
    844 static int
    845 btsco_halt_input(void *hdl)
    846 {
    847 	struct btsco_softc *sc = hdl;
    848 
    849 	DPRINTFN(5, "%s\n", sc->sc_dev.dv_xname);
    850 
    851 	sc->sc_rx_want = 0;
    852 	sc->sc_rx_block = NULL;
    853 	sc->sc_rx_intr = NULL;
    854 	sc->sc_rx_intrarg = NULL;
    855 
    856 	if (sc->sc_rx_mbuf != NULL) {
    857 		m_freem(sc->sc_rx_mbuf);
    858 		sc->sc_rx_mbuf = NULL;
    859 	}
    860 
    861 	return 0;
    862 }
    863 
    864 static int
    865 btsco_getdev(void *hdl, struct audio_device *ret)
    866 {
    867 
    868 	*ret = btsco_device;
    869 	return 0;
    870 }
    871 
    872 static int
    873 btsco_setfd(void *hdl, int fd)
    874 {
    875 /*	struct btsco_softc *sc = hdl;	*/
    876 
    877 	DPRINTF("set %s duplex\n", fd ? "full" : "half");
    878 
    879 	return 0;
    880 }
    881 
    882 static int
    883 btsco_set_port(void *hdl, mixer_ctrl_t *mc)
    884 {
    885 	struct btsco_softc *sc = hdl;
    886 	int err = 0;
    887 
    888 	DPRINTF("%s dev %d type %d\n", sc->sc_dev.dv_xname, mc->dev, mc->type);
    889 
    890 	switch (mc->dev) {
    891 	case BTSCO_VGS:
    892 		if (mc->type != AUDIO_MIXER_VALUE ||
    893 		    mc->un.value.num_channels != 1) {
    894 			err = EINVAL;
    895 			break;
    896 		}
    897 
    898 		sc->sc_vgs = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    899 		break;
    900 
    901 	case BTSCO_VGM:
    902 		if (mc->type != AUDIO_MIXER_VALUE ||
    903 		    mc->un.value.num_channels != 1) {
    904 			err = EINVAL;
    905 			break;
    906 		}
    907 
    908 		sc->sc_vgm = mc->un.value.level[AUDIO_MIXER_LEVEL_MONO];
    909 		break;
    910 
    911 	default:
    912 		err = EINVAL;
    913 		break;
    914 	}
    915 
    916 	return err;
    917 }
    918 
    919 static int
    920 btsco_get_port(void *hdl, mixer_ctrl_t *mc)
    921 {
    922 	struct btsco_softc *sc = hdl;
    923 	int err = 0;
    924 
    925 	DPRINTF("%s dev %d\n", sc->sc_dev.dv_xname, mc->dev);
    926 
    927 	switch (mc->dev) {
    928 	case BTSCO_VGS:
    929 		mc->type = AUDIO_MIXER_VALUE;
    930 		mc->un.value.num_channels = 1;
    931 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_vgs;
    932 		break;
    933 
    934 	case BTSCO_VGM:
    935 		mc->type = AUDIO_MIXER_VALUE;
    936 		mc->un.value.num_channels = 1;
    937 		mc->un.value.level[AUDIO_MIXER_LEVEL_MONO] = sc->sc_vgm;
    938 		break;
    939 
    940 	default:
    941 		err = EINVAL;
    942 		break;
    943 	}
    944 
    945 	return err;
    946 }
    947 
    948 static int
    949 btsco_query_devinfo(void *hdl, mixer_devinfo_t *di)
    950 {
    951 /*	struct btsco_softc *sc = hdl;	*/
    952 	int err = 0;
    953 
    954 	switch(di->index) {
    955 	case BTSCO_VGS:
    956 		di->mixer_class = BTSCO_INPUT_CLASS;
    957 		di->next = di->prev = AUDIO_MIXER_LAST;
    958 		strcpy(di->label.name, AudioNspeaker);
    959 		di->type = AUDIO_MIXER_VALUE;
    960 		strcpy(di->un.v.units.name, AudioNvolume);
    961 		di->un.v.num_channels = 1;
    962 		di->un.v.delta = BTSCO_DELTA;
    963 		break;
    964 
    965 	case BTSCO_VGM:
    966 		di->mixer_class = BTSCO_INPUT_CLASS;
    967 		di->next = di->prev = AUDIO_MIXER_LAST;
    968 		strcpy(di->label.name, AudioNmicrophone);
    969 		di->type = AUDIO_MIXER_VALUE;
    970 		strcpy(di->un.v.units.name, AudioNvolume);
    971 		di->un.v.num_channels = 1;
    972 		di->un.v.delta = BTSCO_DELTA;
    973 		break;
    974 
    975 	case BTSCO_INPUT_CLASS:
    976 		di->mixer_class = BTSCO_INPUT_CLASS;
    977 		di->next = di->prev = AUDIO_MIXER_LAST;
    978 		strcpy(di->label.name, AudioCinputs);
    979 		di->type = AUDIO_MIXER_CLASS;
    980 		break;
    981 
    982 	default:
    983 		err = ENXIO;
    984 		break;
    985 	}
    986 
    987 	return err;
    988 }
    989 
    990 /*
    991  * Allocate Ring Buffers.
    992  */
    993 static void *
    994 btsco_allocm(void *hdl, int direction, size_t size,
    995 		struct malloc_type *type, int flags)
    996 {
    997 	struct btsco_softc *sc = hdl;
    998 	void *addr;
    999 
   1000 	DPRINTF("%s: size %d direction %d\n",
   1001 			sc->sc_dev.dv_xname, size, direction);
   1002 
   1003 	addr = malloc(size, type, flags);
   1004 
   1005 	if (direction == AUMODE_PLAY) {
   1006 		sc->sc_tx_buf = addr;
   1007 		sc->sc_tx_refcnt = 0;
   1008 	}
   1009 
   1010 	return addr;
   1011 }
   1012 
   1013 /*
   1014  * Free Ring Buffers.
   1015  *
   1016  * Because we used external memory for the tx mbufs, we dont
   1017  * want to free the memory until all the mbufs are done with
   1018  *
   1019  * Just to be sure, dont free if something is still pending.
   1020  * This would be a memory leak but at least there is a warning..
   1021  */
   1022 static void
   1023 btsco_freem(void *hdl, void *addr, struct malloc_type *type)
   1024 {
   1025 	struct btsco_softc *sc = hdl;
   1026 	int count = hz / 2;
   1027 
   1028 	if (addr == sc->sc_tx_buf) {
   1029 		DPRINTF("%s: tx_refcnt=%d\n",
   1030 			sc->sc_dev.dv_xname, sc->sc_tx_refcnt);
   1031 
   1032 		sc->sc_tx_buf = NULL;
   1033 
   1034 		while (sc->sc_tx_refcnt> 0 && count-- > 0)
   1035 			tsleep(sc, PWAIT, "drain", 1);
   1036 
   1037 		if (sc->sc_tx_refcnt > 0) {
   1038 			printf("%s: ring buffer unreleased!\n", sc->sc_dev.dv_xname);
   1039 			return;
   1040 		}
   1041 	}
   1042 
   1043 	free(addr, type);
   1044 }
   1045 
   1046 static int
   1047 btsco_get_props(void *hdl)
   1048 {
   1049 
   1050 	return AUDIO_PROP_FULLDUPLEX;
   1051 }
   1052 
   1053 /*
   1054  * Handle private ioctl. We pass information out about how to talk
   1055  * to the device and mixer.
   1056  */
   1057 static int
   1058 btsco_dev_ioctl(void *hdl, u_long cmd, caddr_t addr, int flag, struct lwp *l)
   1059 {
   1060 	struct btsco_softc *sc = hdl;
   1061 	struct btsco_info *bi = (struct btsco_info *)addr;
   1062 	int err = 0;
   1063 
   1064 	DPRINTF("%s cmd 0x%lx flag %d\n", sc->sc_dev.dv_xname, cmd, flag);
   1065 
   1066 	switch (cmd) {
   1067 	case BTSCO_GETINFO:
   1068 		memset(bi, 0, sizeof(*bi));
   1069 		bdaddr_copy(&bi->laddr, &sc->sc_laddr);
   1070 		bdaddr_copy(&bi->raddr, &sc->sc_raddr);
   1071 		bi->channel = sc->sc_channel;
   1072 		bi->vgs = BTSCO_VGS;
   1073 		bi->vgm = BTSCO_VGM;
   1074 		break;
   1075 
   1076 	default:
   1077 		err = EPASSTHROUGH;
   1078 		break;
   1079 	}
   1080 
   1081 	return err;
   1082 }
   1083 
   1084 
   1085 /*****************************************************************************
   1086  *
   1087  *	misc btsco functions
   1088  *
   1089  */
   1090 
   1091 /*
   1092  * Our transmit interrupt. This is triggered when a new block is to be
   1093  * sent.  We send mtu sized chunks of the block as mbufs with external
   1094  * storage to sco_send()
   1095  */
   1096 static void
   1097 btsco_intr(void *arg)
   1098 {
   1099 	struct btsco_softc *sc = arg;
   1100 	struct mbuf *m;
   1101 	uint8_t *block;
   1102 	int mlen, size;
   1103 
   1104 	DPRINTFN(10, "%s block %p size %d\n",
   1105 		sc->sc_dev.dv_xname, sc->sc_tx_block, sc->sc_tx_size);
   1106 
   1107 	if (sc->sc_sco == NULL)
   1108 		return;		/* connection is lost */
   1109 
   1110 	block = sc->sc_tx_block;
   1111 	size = sc->sc_tx_size;
   1112 	sc->sc_tx_block = NULL;
   1113 	sc->sc_tx_size = 0;
   1114 
   1115 	while (size > 0) {
   1116 		MGETHDR(m, M_DONTWAIT, MT_DATA);
   1117 		if (m == NULL)
   1118 			break;
   1119 
   1120 		mlen = MIN(sc->sc_mtu, size);
   1121 
   1122 		/* I think M_DEVBUF is true but not relevant */
   1123 		MEXTADD(m, block, mlen, M_DEVBUF, btsco_extfree, sc);
   1124 		if ((m->m_flags & M_EXT) == 0) {
   1125 			m_free(m);
   1126 			break;
   1127 		}
   1128 		sc->sc_tx_refcnt++;
   1129 
   1130 		m->m_pkthdr.len = m->m_len = mlen;
   1131 		sc->sc_tx_pending++;
   1132 
   1133 		if (sco_send(sc->sc_sco, m) > 0) {
   1134 			sc->sc_tx_pending--;
   1135 			break;
   1136 		}
   1137 
   1138 		block += mlen;
   1139 		size -= mlen;
   1140 	}
   1141 }
   1142 
   1143 /*
   1144  * Release the mbuf, we keep a reference count on the tx buffer so
   1145  * that we dont release it before its free.
   1146  */
   1147 static void
   1148 btsco_extfree(struct mbuf *m, caddr_t addr, size_t size, void *arg)
   1149 {
   1150 	struct btsco_softc *sc = arg;
   1151 
   1152 	if (m != NULL)
   1153 		pool_cache_put(&mbpool_cache, m);
   1154 
   1155 	sc->sc_tx_refcnt--;
   1156 }
   1157