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