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