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