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