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