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