bcm2835_vcaudio.c revision 1.8 1 /* $NetBSD: bcm2835_vcaudio.c,v 1.8 2015/03/13 22:48:41 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2013 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 /*
30 * VideoCore audio interface
31 */
32
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: bcm2835_vcaudio.c,v 1.8 2015/03/13 22:48:41 jmcneill Exp $");
35
36 #include <sys/param.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/device.h>
40 #include <sys/conf.h>
41 #include <sys/bus.h>
42 #include <sys/kmem.h>
43
44 #include <sys/audioio.h>
45 #include <dev/audio_if.h>
46 #include <dev/auconv.h>
47 #include <dev/auvolconv.h>
48
49 #include <interface/compat/vchi_bsd.h>
50 #include <interface/vchiq_arm/vchiq_netbsd.h>
51 #include <interface/vchi/vchi.h>
52
53 #include "bcm2835_vcaudioreg.h"
54
55 #define vol2pct(vol) (((vol) * 100) / 255)
56
57 enum {
58 VCAUDIO_OUTPUT_CLASS,
59 VCAUDIO_INPUT_CLASS,
60 VCAUDIO_OUTPUT_MASTER_VOLUME,
61 VCAUDIO_INPUT_DAC_VOLUME,
62 VCAUDIO_OUTPUT_HEADPHONE_VOLUME,
63 VCAUDIO_OUTPUT_SELECT,
64 VCAUDIO_ENUM_LAST,
65 };
66
67 enum vcaudio_dest {
68 VCAUDIO_DEST_AUTO = 0,
69 VCAUDIO_DEST_HP = 1,
70 VCAUDIO_DEST_HDMI = 2,
71 };
72
73
74 /*
75 * Standard message size is 4000 bytes and VCHIQ can accept 16 messages.
76 *
77 * 4000 bytes of 16bit 48kHz stereo is approximately 21ms.
78 *
79 * We get complete messages at ~10ms intervals.
80 *
81 * Setting blocksize to 2 x 4000 means that we send approx 42ms of audio. We
82 * prefill by two blocks before starting audio meaning we have 83ms of latency.
83 */
84
85 #define VCAUDIO_MSGSIZE 4000
86 #define VCAUDIO_NUMMSGS 2
87 #define VCAUDIO_BLOCKSIZE (VCAUDIO_MSGSIZE * VCAUDIO_NUMMSGS)
88 #define VCAUDIO_BUFFERSIZE 128000
89 #define VCAUDIO_PREFILLCOUNT 2
90
91 struct vcaudio_softc {
92 device_t sc_dev;
93 device_t sc_audiodev;
94
95 lwp_t *sc_lwp;
96
97 kmutex_t sc_lock;
98 kmutex_t sc_intr_lock;
99 kcondvar_t sc_datacv;
100
101 kmutex_t sc_msglock;
102 kcondvar_t sc_msgcv;
103
104 struct audio_format sc_format;
105 struct audio_encoding_set *sc_encodings;
106
107 void (*sc_pint)(void *);
108 void *sc_pintarg;
109 audio_params_t sc_pparam;
110 bool sc_started;
111 int sc_pblkcnt; // prefill block count
112 int sc_abytes; // available bytes
113 int sc_pbytes; // played bytes
114 off_t sc_ppos;
115 void *sc_pstart;
116 void *sc_pend;
117 int sc_pblksize;
118
119 bool sc_msgdone;
120 int sc_success;
121
122 VCHI_INSTANCE_T sc_instance;
123 VCHI_CONNECTION_T sc_connection;
124 VCHI_SERVICE_HANDLE_T sc_service;
125
126 short sc_peer_version;
127
128 int sc_hpvol;
129 enum vcaudio_dest sc_dest;
130
131 uint8_t sc_swvol;
132 };
133
134 static int vcaudio_match(device_t, cfdata_t, void *);
135 static void vcaudio_attach(device_t, device_t, void *);
136 static int vcaudio_rescan(device_t, const char *, const int *);
137 static void vcaudio_childdet(device_t, device_t);
138
139 static int vcaudio_init(struct vcaudio_softc *);
140 static void vcaudio_service_callback(void *,
141 const VCHI_CALLBACK_REASON_T, void *);
142 static int vcaudio_msg_sync(struct vcaudio_softc *, VC_AUDIO_MSG_T *,
143 size_t);
144 static void vcaudio_worker(void *);
145
146 static int vcaudio_open(void *, int);
147 static void vcaudio_close(void *);
148 static int vcaudio_query_encoding(void *, struct audio_encoding *);
149 static int vcaudio_set_params(void *, int, int,
150 audio_params_t *, audio_params_t *,
151 stream_filter_list_t *, stream_filter_list_t *);
152 static int vcaudio_halt_output(void *);
153 static int vcaudio_halt_input(void *);
154 static int vcaudio_set_port(void *, mixer_ctrl_t *);
155 static int vcaudio_get_port(void *, mixer_ctrl_t *);
156 static int vcaudio_query_devinfo(void *, mixer_devinfo_t *);
157 static int vcaudio_getdev(void *, struct audio_device *);
158 static int vcaudio_get_props(void *);
159
160 static int vcaudio_round_blocksize(void *, int, int,
161 const audio_params_t *);
162 static size_t vcaudio_round_buffersize(void *, int, size_t);
163
164 static int vcaudio_trigger_output(void *, void *, void *, int,
165 void (*)(void *), void *, const audio_params_t *);
166 static int vcaudio_trigger_input(void *, void *, void *, int,
167 void (*)(void *), void *, const audio_params_t *);
168
169 static void vcaudio_get_locks(void *, kmutex_t **, kmutex_t **);
170
171 static stream_filter_t *vcaudio_swvol_filter(struct audio_softc *,
172 const audio_params_t *, const audio_params_t *);
173 static void vcaudio_swvol_dtor(stream_filter_t *);
174
175 static const struct audio_hw_if vcaudio_hw_if = {
176 .open = vcaudio_open,
177 .close = vcaudio_close,
178 .query_encoding = vcaudio_query_encoding,
179 .set_params = vcaudio_set_params,
180 .halt_output = vcaudio_halt_output,
181 .halt_input = vcaudio_halt_input,
182 .getdev = vcaudio_getdev,
183 .set_port = vcaudio_set_port,
184 .get_port = vcaudio_get_port,
185 .query_devinfo = vcaudio_query_devinfo,
186 .get_props = vcaudio_get_props,
187 .round_blocksize = vcaudio_round_blocksize,
188 .round_buffersize = vcaudio_round_buffersize,
189 .trigger_output = vcaudio_trigger_output,
190 .trigger_input = vcaudio_trigger_input,
191 .get_locks = vcaudio_get_locks,
192 };
193
194 CFATTACH_DECL2_NEW(vcaudio, sizeof(struct vcaudio_softc),
195 vcaudio_match, vcaudio_attach, NULL, NULL, vcaudio_rescan,
196 vcaudio_childdet);
197
198 static int
199 vcaudio_match(device_t parent, cfdata_t match, void *aux)
200 {
201 struct vchiq_attach_args *vaa = aux;
202
203 return !strcmp(vaa->vaa_name, "AUDS");
204 }
205
206 static void
207 vcaudio_attach(device_t parent, device_t self, void *aux)
208 {
209 struct vcaudio_softc *sc = device_private(self);
210 int error;
211
212 sc->sc_dev = self;
213 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
214 mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_NONE);
215 mutex_init(&sc->sc_msglock, MUTEX_DEFAULT, IPL_NONE);
216 cv_init(&sc->sc_msgcv, "msg");
217 cv_init(&sc->sc_datacv, "data");
218 sc->sc_success = -1;
219
220 error = kthread_create(PRI_BIO, KTHREAD_MPSAFE, NULL, vcaudio_worker,
221 sc, &sc->sc_lwp, "vcaudio");
222 if (error) {
223 aprint_error(": couldn't create thread (%d)\n", error);
224 return;
225 }
226
227 aprint_naive("\n");
228 aprint_normal(": auds\n");
229
230 error = vcaudio_rescan(self, NULL, NULL);
231 if (error)
232 aprint_error_dev(self, "not configured\n");
233
234 }
235
236 static int
237 vcaudio_rescan(device_t self, const char *ifattr, const int *locs)
238 {
239 struct vcaudio_softc *sc = device_private(self);
240 int error;
241
242 if (ifattr_match(ifattr, "audiobus") && sc->sc_audiodev == NULL) {
243 error = vcaudio_init(sc);
244 if (error) {
245 return error;
246 }
247
248 sc->sc_audiodev = audio_attach_mi(&vcaudio_hw_if,
249 sc, sc->sc_dev);
250 }
251 return 0;
252 }
253
254 static void
255 vcaudio_childdet(device_t self, device_t child)
256 {
257 struct vcaudio_softc *sc = device_private(self);
258
259 if (sc->sc_audiodev == child)
260 sc->sc_audiodev = NULL;
261 }
262
263 static int
264 vcaudio_init(struct vcaudio_softc *sc)
265 {
266 VC_AUDIO_MSG_T msg;
267 int error;
268
269 sc->sc_swvol = 255;
270 sc->sc_hpvol = 128;
271 sc->sc_dest = VCAUDIO_DEST_AUTO;
272
273 sc->sc_format.mode = AUMODE_PLAY|AUMODE_RECORD;
274 sc->sc_format.encoding = AUDIO_ENCODING_SLINEAR_LE;
275 sc->sc_format.validbits = 16;
276 sc->sc_format.precision = 16;
277 sc->sc_format.channels = 2;
278 sc->sc_format.channel_mask = AUFMT_STEREO;
279 sc->sc_format.frequency_type = 0;
280 sc->sc_format.frequency[0] = 48000;
281 sc->sc_format.frequency[1] = 48000;
282
283 error = auconv_create_encodings(&sc->sc_format, 1, &sc->sc_encodings);
284 if (error) {
285 aprint_error_dev(sc->sc_dev,
286 "couldn't create encodings (error=%d)\n", error);
287 return error;
288 }
289
290 error = vchi_initialise(&sc->sc_instance);
291 if (error) {
292 aprint_error_dev(sc->sc_dev,
293 "couldn't init vchi instance (%d)\n", error);
294 return EIO;
295 }
296
297 error = vchi_connect(NULL, 0, sc->sc_instance);
298 if (error) {
299 aprint_error_dev(sc->sc_dev,
300 "couldn't connect vchi (%d)\n", error);
301 return EIO;
302 }
303
304 SERVICE_CREATION_T setup = {
305 .version = VCHI_VERSION(VC_AUDIOSERV_VER),
306 .service_id = VC_AUDIO_SERVER_NAME,
307 .connection = &sc->sc_connection,
308 .rx_fifo_size = 0,
309 .tx_fifo_size = 0,
310 .callback = vcaudio_service_callback,
311 .callback_param = sc,
312 .want_unaligned_bulk_rx = 1,
313 .want_unaligned_bulk_tx = 1,
314 .want_crc = 0,
315 };
316
317 error = vchi_service_open(sc->sc_instance, &setup, &sc->sc_service);
318 if (error) {
319 aprint_error_dev(sc->sc_dev, "couldn't open service (%d)\n",
320 error);
321 return EIO;
322 }
323
324 vchi_get_peer_version(sc->sc_service, &sc->sc_peer_version);
325
326 if (sc->sc_peer_version < 2) {
327 aprint_error_dev(sc->sc_dev,
328 "peer version (%d) is less than the required version (2)\n",
329 sc->sc_peer_version);
330 return EINVAL;
331 }
332
333 memset(&msg, 0, sizeof(msg));
334 msg.type = VC_AUDIO_MSG_TYPE_OPEN;
335 error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
336 VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
337 if (error) {
338 aprint_error_dev(sc->sc_dev,
339 "couldn't send OPEN message (%d)\n", error);
340 }
341
342 memset(&msg, 0, sizeof(msg));
343 msg.type = VC_AUDIO_MSG_TYPE_CONFIG;
344 msg.u.config.channels = 2;
345 msg.u.config.samplerate = 48000;
346 msg.u.config.bps = 16;
347 error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
348 if (error) {
349 aprint_error_dev(sc->sc_dev,
350 "couldn't send CONFIG message (%d)\n", error);
351 }
352
353 memset(&msg, 0, sizeof(msg));
354 msg.type = VC_AUDIO_MSG_TYPE_CONTROL;
355 msg.u.control.volume = vol2pct(sc->sc_hpvol);
356 msg.u.control.dest = sc->sc_dest;
357 error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
358 if (error) {
359 aprint_error_dev(sc->sc_dev,
360 "couldn't send CONTROL message (%d)\n", error);
361 }
362
363 vchi_service_release(sc->sc_service);
364
365 return 0;
366 }
367
368 static void
369 vcaudio_service_callback(void *priv, const VCHI_CALLBACK_REASON_T reason,
370 void *msg_handle)
371 {
372 struct vcaudio_softc *sc = priv;
373 VC_AUDIO_MSG_T msg;
374 int32_t msglen = 0;
375 int error;
376 void (*intr)(void *) = NULL;
377 void *intrarg = NULL;
378
379 if (sc == NULL || reason != VCHI_CALLBACK_MSG_AVAILABLE)
380 return;
381
382 memset(&msg, 0, sizeof(msg));
383 error = vchi_msg_dequeue(sc->sc_service, &msg, sizeof(msg), &msglen,
384 VCHI_FLAGS_NONE);
385 if (error) {
386 device_printf(sc->sc_dev, "couldn't dequeue msg (%d)\n",
387 error);
388 return;
389 }
390
391 switch (msg.type) {
392 case VC_AUDIO_MSG_TYPE_RESULT:
393 mutex_enter(&sc->sc_msglock);
394 sc->sc_success = msg.u.result.success;
395 sc->sc_msgdone = true;
396 cv_broadcast(&sc->sc_msgcv);
397 mutex_exit(&sc->sc_msglock);
398 break;
399
400 case VC_AUDIO_MSG_TYPE_COMPLETE:
401 intr = msg.u.complete.callback;
402 intrarg = msg.u.complete.cookie;
403 if (intr && intrarg) {
404 int count = msg.u.complete.count & 0xffff;
405 int perr = (msg.u.complete.count & __BIT(30)) != 0;
406 bool sched = false;
407
408 mutex_enter(&sc->sc_intr_lock);
409
410 if (count > 0) {
411 sc->sc_pbytes += count;
412 }
413 if (perr && sc->sc_started) {
414 #ifdef VCAUDIO_DEBUG
415 device_printf(sc->sc_dev, "underrun\n");
416 #endif
417 sched = true;
418 }
419 if (sc->sc_pbytes >= sc->sc_pblksize) {
420 sc->sc_pbytes -= sc->sc_pblksize;
421 sched = true;
422 }
423
424 if (sched) {
425 intr(intrarg);
426 sc->sc_abytes += sc->sc_pblksize;
427 cv_signal(&sc->sc_datacv);
428 }
429 mutex_exit(&sc->sc_intr_lock);
430 }
431 break;
432 default:
433 break;
434 }
435 }
436
437 static void
438 vcaudio_worker(void *priv)
439 {
440 struct vcaudio_softc *sc = priv;
441 VC_AUDIO_MSG_T msg;
442 void (*intr)(void *);
443 void *intrarg;
444 void *block;
445 int error, resid, off, nb, count;
446
447 mutex_enter(&sc->sc_intr_lock);
448
449 while (true) {
450 intr = sc->sc_pint;
451 intrarg = sc->sc_pintarg;
452
453 if (intr == NULL || intrarg == NULL) {
454 cv_wait_sig(&sc->sc_datacv, &sc->sc_intr_lock);
455 continue;
456 }
457
458 KASSERT(sc->sc_pblksize != 0);
459
460 if (sc->sc_abytes < sc->sc_pblksize) {
461 cv_wait_sig(&sc->sc_datacv, &sc->sc_intr_lock);
462 continue;
463 }
464 count = sc->sc_pblksize;
465
466 memset(&msg, 0, sizeof(msg));
467 msg.type = VC_AUDIO_MSG_TYPE_WRITE;
468 msg.u.write.max_packet = VCAUDIO_MSGSIZE;
469 msg.u.write.count = count;
470 msg.u.write.callback = intr;
471 msg.u.write.cookie = intrarg;
472 msg.u.write.silence = 0;
473
474 block = (uint8_t *)sc->sc_pstart + sc->sc_ppos;
475 resid = count;
476 off = 0;
477
478 vchi_service_use(sc->sc_service);
479
480 error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
481 VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
482 if (error) {
483 printf("%s: failed to write (%d)\n", __func__, error);
484 goto done;
485 }
486
487 while (resid > 0) {
488 nb = min(resid, msg.u.write.max_packet);
489 error = vchi_msg_queue(sc->sc_service,
490 (char *)block + off, nb,
491 VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
492 if (error) {
493 /* XXX What to do here? */
494 device_printf(sc->sc_dev,
495 "failed to queue data (%d)\n", error);
496 goto done;
497 }
498 off += nb;
499 resid -= nb;
500 }
501
502 sc->sc_abytes -= count;
503 sc->sc_ppos += count;
504 if ((uint8_t *)sc->sc_pstart + sc->sc_ppos >=
505 (uint8_t *)sc->sc_pend)
506 sc->sc_ppos = 0;
507
508 if (!sc->sc_started) {
509 ++sc->sc_pblkcnt;
510
511 if (sc->sc_pblkcnt == VCAUDIO_PREFILLCOUNT) {
512
513 memset(&msg, 0, sizeof(msg));
514 msg.type = VC_AUDIO_MSG_TYPE_START;
515 error = vchi_msg_queue(sc->sc_service, &msg,
516 sizeof(msg), VCHI_FLAGS_BLOCK_UNTIL_QUEUED,
517 NULL);
518 if (error) {
519 device_printf(sc->sc_dev,
520 "failed to start (%d)\n", error);
521 goto done;
522 }
523 sc->sc_started = true;
524 sc->sc_pbytes = 0;
525 } else {
526 intr(intrarg);
527 sc->sc_abytes += sc->sc_pblksize;
528 }
529 }
530
531 done:
532 vchi_service_release(sc->sc_service);
533 }
534 }
535
536 static int
537 vcaudio_msg_sync(struct vcaudio_softc *sc, VC_AUDIO_MSG_T *msg, size_t msglen)
538 {
539 int error = 0;
540
541 mutex_enter(&sc->sc_msglock);
542
543 sc->sc_success = -1;
544 sc->sc_msgdone = false;
545
546 error = vchi_msg_queue(sc->sc_service, msg, msglen,
547 VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
548 if (error) {
549 printf("%s: failed to queue message (%d)\n", __func__, error);
550 goto done;
551 }
552
553 while (!sc->sc_msgdone) {
554 error = cv_wait_sig(&sc->sc_msgcv, &sc->sc_msglock);
555 if (error)
556 break;
557 }
558 if (sc->sc_success != 0)
559 error = EIO;
560 done:
561 mutex_exit(&sc->sc_msglock);
562
563 return error;
564 }
565
566 static int
567 vcaudio_open(void *priv, int flags)
568 {
569 return 0;
570 }
571
572 static void
573 vcaudio_close(void *priv)
574 {
575 }
576
577 static int
578 vcaudio_query_encoding(void *priv, struct audio_encoding *ae)
579 {
580 struct vcaudio_softc *sc = priv;
581
582 return auconv_query_encoding(sc->sc_encodings, ae);
583 }
584
585 static int
586 vcaudio_set_params(void *priv, int setmode, int usemode,
587 audio_params_t *play, audio_params_t *rec,
588 stream_filter_list_t *pfil, stream_filter_list_t *rfil)
589 {
590 struct vcaudio_softc *sc = priv;
591 int index;
592
593 if (play && (setmode & AUMODE_PLAY)) {
594 index = auconv_set_converter(&sc->sc_format, 1,
595 AUMODE_PLAY, play, true, pfil);
596 if (index < 0)
597 return EINVAL;
598 if (pfil->req_size > 0)
599 play = &pfil->filters[0].param;
600 pfil->prepend(pfil, vcaudio_swvol_filter, play);
601 }
602
603 return 0;
604 }
605
606 static int
607 vcaudio_halt_output(void *priv)
608 {
609 struct vcaudio_softc *sc = priv;
610 VC_AUDIO_MSG_T msg;
611 int error = 0;
612
613 KASSERT(mutex_owned(&sc->sc_intr_lock));
614
615 vchi_service_use(sc->sc_service);
616 memset(&msg, 0, sizeof(msg));
617 msg.type = VC_AUDIO_MSG_TYPE_STOP;
618 msg.u.stop.draining = 0;
619 error = vchi_msg_queue(sc->sc_service, &msg, sizeof(msg),
620 VCHI_FLAGS_BLOCK_UNTIL_QUEUED, NULL);
621 if (error) {
622 device_printf(sc->sc_dev, "couldn't send STOP message (%d)\n",
623 error);
624 }
625 vchi_service_release(sc->sc_service);
626
627 sc->sc_pint = NULL;
628 sc->sc_pintarg = NULL;
629 sc->sc_started = false;
630
631 #ifdef VCAUDIO_DEBUG
632 device_printf(sc->sc_dev, "halting output\n");
633 #endif
634
635 return error;
636 }
637
638 static int
639 vcaudio_halt_input(void *priv)
640 {
641 return EINVAL;
642 }
643
644 static int
645 vcaudio_set_port(void *priv, mixer_ctrl_t *mc)
646 {
647 struct vcaudio_softc *sc = priv;
648 VC_AUDIO_MSG_T msg;
649 int error;
650
651 switch (mc->dev) {
652 case VCAUDIO_OUTPUT_MASTER_VOLUME:
653 case VCAUDIO_INPUT_DAC_VOLUME:
654 sc->sc_swvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
655 return 0;
656 case VCAUDIO_OUTPUT_HEADPHONE_VOLUME:
657 sc->sc_hpvol = mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT];
658
659 vchi_service_use(sc->sc_service);
660
661 memset(&msg, 0, sizeof(msg));
662 msg.type = VC_AUDIO_MSG_TYPE_CONTROL;
663 msg.u.control.volume = vol2pct(sc->sc_hpvol);
664 msg.u.control.dest = sc->sc_dest;
665
666 error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
667 if (error) {
668 device_printf(sc->sc_dev,
669 "couldn't send CONTROL message (%d)\n", error);
670 }
671
672 vchi_service_release(sc->sc_service);
673
674 return error;
675 case VCAUDIO_OUTPUT_SELECT:
676 if (mc->un.ord < 0 || mc->un.ord > 2)
677 return EINVAL;
678
679 sc->sc_dest = mc->un.ord;
680
681 vchi_service_use(sc->sc_service);
682
683 memset(&msg, 0, sizeof(msg));
684 msg.type = VC_AUDIO_MSG_TYPE_CONTROL;
685 msg.u.control.volume = vol2pct(sc->sc_hpvol);
686 msg.u.control.dest = sc->sc_dest;
687
688 error = vcaudio_msg_sync(sc, &msg, sizeof(msg));
689 if (error) {
690 device_printf(sc->sc_dev,
691 "couldn't send CONTROL message (%d)\n", error);
692 }
693
694 vchi_service_release(sc->sc_service);
695
696 return error;
697 }
698 return ENXIO;
699 }
700
701 static int
702 vcaudio_get_port(void *priv, mixer_ctrl_t *mc)
703 {
704 struct vcaudio_softc *sc = priv;
705 uint8_t vol = sc->sc_swvol;
706
707 switch (mc->dev) {
708 case VCAUDIO_OUTPUT_MASTER_VOLUME:
709 case VCAUDIO_INPUT_DAC_VOLUME:
710 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] = vol;
711 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] = vol;
712 return 0;
713 case VCAUDIO_OUTPUT_HEADPHONE_VOLUME:
714 mc->un.value.level[AUDIO_MIXER_LEVEL_LEFT] =
715 mc->un.value.level[AUDIO_MIXER_LEVEL_RIGHT] =
716 sc->sc_hpvol;
717 return 0;
718 case VCAUDIO_OUTPUT_SELECT:
719 mc->un.ord = sc->sc_dest;
720 return 0;
721 }
722 return ENXIO;
723 }
724
725 static int
726 vcaudio_query_devinfo(void *priv, mixer_devinfo_t *di)
727 {
728 switch (di->index) {
729 case VCAUDIO_OUTPUT_CLASS:
730 di->mixer_class = VCAUDIO_OUTPUT_CLASS;
731 strcpy(di->label.name, AudioCoutputs);
732 di->type = AUDIO_MIXER_CLASS;
733 di->next = di->prev = AUDIO_MIXER_LAST;
734 return 0;
735 case VCAUDIO_INPUT_CLASS:
736 di->mixer_class = VCAUDIO_INPUT_CLASS;
737 strcpy(di->label.name, AudioCinputs);
738 di->type = AUDIO_MIXER_CLASS;
739 di->next = di->prev = AUDIO_MIXER_LAST;
740 return 0;
741 case VCAUDIO_OUTPUT_MASTER_VOLUME:
742 di->mixer_class = VCAUDIO_OUTPUT_CLASS;
743 strcpy(di->label.name, AudioNmaster);
744 di->type = AUDIO_MIXER_VALUE;
745 di->next = di->prev = AUDIO_MIXER_LAST;
746 di->un.v.num_channels = 2;
747 strcpy(di->un.v.units.name, AudioNvolume);
748 return 0;
749 case VCAUDIO_OUTPUT_HEADPHONE_VOLUME:
750 di->mixer_class = VCAUDIO_OUTPUT_CLASS;
751 strcpy(di->label.name, AudioNheadphone);
752 di->type = AUDIO_MIXER_VALUE;
753 di->next = di->prev = AUDIO_MIXER_LAST;
754 di->un.v.num_channels = 2;
755 strcpy(di->un.v.units.name, AudioNvolume);
756 return 0;
757 case VCAUDIO_INPUT_DAC_VOLUME:
758 di->mixer_class = VCAUDIO_INPUT_CLASS;
759 strcpy(di->label.name, AudioNdac);
760 di->type = AUDIO_MIXER_VALUE;
761 di->next = di->prev = AUDIO_MIXER_LAST;
762 di->un.v.num_channels = 2;
763 strcpy(di->un.v.units.name, AudioNvolume);
764 return 0;
765 case VCAUDIO_OUTPUT_SELECT:
766 di->mixer_class = VCAUDIO_OUTPUT_CLASS;
767 strcpy(di->label.name, AudioNselect);
768 di->type = AUDIO_MIXER_ENUM;
769 di->next = di->prev = AUDIO_MIXER_LAST;
770 di->un.e.num_mem = 3;
771 di->un.e.member[0].ord = 0;
772 strcpy(di->un.e.member[0].label.name, "auto");
773 di->un.e.member[1].ord = 1;
774 strcpy(di->un.e.member[1].label.name, AudioNheadphone);
775 di->un.e.member[2].ord = 2;
776 strcpy(di->un.e.member[2].label.name, "hdmi");
777 return 0;
778 }
779
780 return ENXIO;
781 }
782
783 static int
784 vcaudio_getdev(void *priv, struct audio_device *audiodev)
785 {
786 struct vcaudio_softc *sc = priv;
787
788 snprintf(audiodev->name, sizeof(audiodev->name), "vchiq auds");
789 snprintf(audiodev->version, sizeof(audiodev->version),
790 "%d", sc->sc_peer_version);
791 snprintf(audiodev->config, sizeof(audiodev->config), "vcaudio");
792
793 return 0;
794 }
795
796 static int
797 vcaudio_get_props(void *priv)
798 {
799 return AUDIO_PROP_PLAYBACK|AUDIO_PROP_CAPTURE|AUDIO_PROP_INDEPENDENT;
800 }
801
802 static int
803 vcaudio_round_blocksize(void *priv, int bs, int mode,
804 const audio_params_t *params)
805 {
806 return VCAUDIO_BLOCKSIZE;
807 }
808
809 static size_t
810 vcaudio_round_buffersize(void *priv, int direction, size_t bufsize)
811 {
812
813 return VCAUDIO_BUFFERSIZE;
814 }
815
816 static int
817 vcaudio_trigger_output(void *priv, void *start, void *end, int blksize,
818 void (*intr)(void *), void *intrarg, const audio_params_t *params)
819 {
820 struct vcaudio_softc *sc = priv;
821
822 ASSERT_SLEEPABLE();
823 KASSERT(mutex_owned(&sc->sc_intr_lock));
824
825 sc->sc_pparam = *params;
826 sc->sc_pint = intr;
827 sc->sc_pintarg = intrarg;
828 sc->sc_ppos = 0;
829 sc->sc_pstart = start;
830 sc->sc_pend = end;
831 sc->sc_pblksize = blksize;
832 sc->sc_pblkcnt = 0;
833 sc->sc_pbytes = 0;
834 sc->sc_abytes = blksize;
835
836 cv_signal(&sc->sc_datacv);
837
838 return 0;
839 }
840
841 static int
842 vcaudio_trigger_input(void *priv, void *start, void *end, int blksize,
843 void (*intr)(void *), void *intrarg, const audio_params_t *params)
844 {
845 return EINVAL;
846 }
847
848 static void
849 vcaudio_get_locks(void *priv, kmutex_t **intr, kmutex_t **thread)
850 {
851 struct vcaudio_softc *sc = priv;
852
853 *intr = &sc->sc_intr_lock;
854 *thread = &sc->sc_lock;
855 }
856
857 static stream_filter_t *
858 vcaudio_swvol_filter(struct audio_softc *asc,
859 const audio_params_t *from, const audio_params_t *to)
860 {
861 auvolconv_filter_t *this;
862 device_t dev = audio_get_device(asc);
863 struct vcaudio_softc *sc = device_private(dev);
864
865 this = kmem_alloc(sizeof(auvolconv_filter_t), KM_SLEEP);
866 this->base.base.fetch_to = auvolconv_slinear16_le_fetch_to;
867 this->base.dtor = vcaudio_swvol_dtor;
868 this->base.set_fetcher = stream_filter_set_fetcher;
869 this->base.set_inputbuffer = stream_filter_set_inputbuffer;
870 this->vol = &sc->sc_swvol;
871
872 return (stream_filter_t *)this;
873 }
874
875 static void
876 vcaudio_swvol_dtor(stream_filter_t *this)
877 {
878 if (this)
879 kmem_free(this, sizeof(auvolconv_filter_t));
880 }
881