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