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