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