1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2011 Broadcom Corporation. All rights reserved. */
4 #include <linux/device.h>
5 #include <sound/core.h>
6 #include <sound/initval.h>
9 #include <linux/interrupt.h>
11 #include <linux/file.h>
13 #include <linux/syscalls.h>
14 #include <linux/uaccess.h>
15 #include <linux/slab.h>
16 #include <linux/delay.h>
17 #include <linux/atomic.h>
18 #include <linux/module.h>
19 #include <linux/completion.h>
23 /* ---- Include Files -------------------------------------------------------- */
25 #include "vc_vchi_audioserv_defs.h"
27 /* ---- Private Constants and Types ------------------------------------------ */
29 #define BCM2835_AUDIO_STOP 0
30 #define BCM2835_AUDIO_START 1
31 #define BCM2835_AUDIO_WRITE 2
33 /* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
34 #ifdef AUDIO_DEBUG_ENABLE
35 #define LOG_ERR(fmt, arg...) pr_err("%s:%d " fmt, __func__, __LINE__, ##arg)
36 #define LOG_WARN(fmt, arg...) pr_info("%s:%d " fmt, __func__, __LINE__, ##arg)
37 #define LOG_INFO(fmt, arg...) pr_info("%s:%d " fmt, __func__, __LINE__, ##arg)
38 #define LOG_DBG(fmt, arg...) pr_info("%s:%d " fmt, __func__, __LINE__, ##arg)
40 #define LOG_ERR(fmt, arg...) pr_err("%s:%d " fmt, __func__, __LINE__, ##arg)
41 #define LOG_WARN(fmt, arg...) no_printk(fmt, ##arg)
42 #define LOG_INFO(fmt, arg...) no_printk(fmt, ##arg)
43 #define LOG_DBG(fmt, arg...) no_printk(fmt, ##arg)
46 struct bcm2835_audio_instance {
47 unsigned int num_connections;
48 VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
49 struct completion msg_avail_comp;
50 struct mutex vchi_mutex;
51 struct bcm2835_alsa_stream *alsa_stream;
56 static bool force_bulk;
58 /* ---- Private Variables ---------------------------------------------------- */
60 /* ---- Private Function Prototypes ------------------------------------------ */
62 /* ---- Private Functions ---------------------------------------------------- */
64 static int bcm2835_audio_stop_worker(struct bcm2835_alsa_stream *alsa_stream);
65 static int bcm2835_audio_start_worker(struct bcm2835_alsa_stream *alsa_stream);
66 static int bcm2835_audio_write_worker(struct bcm2835_alsa_stream *alsa_stream,
67 unsigned int count, void *src);
69 // Routine to send a message across a service
72 bcm2835_vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
76 return vchi_queue_kernel_message(handle,
81 static const u32 BCM2835_AUDIO_WRITE_COOKIE1 = ('B' << 24 | 'C' << 16 |
83 static const u32 BCM2835_AUDIO_WRITE_COOKIE2 = ('D' << 24 | 'A' << 16 |
86 struct bcm2835_audio_work {
87 struct work_struct my_work;
88 struct bcm2835_alsa_stream *alsa_stream;
94 static void my_wq_function(struct work_struct *work)
96 struct bcm2835_audio_work *w =
97 container_of(work, struct bcm2835_audio_work, my_work);
101 case BCM2835_AUDIO_START:
102 ret = bcm2835_audio_start_worker(w->alsa_stream);
104 case BCM2835_AUDIO_STOP:
105 ret = bcm2835_audio_stop_worker(w->alsa_stream);
107 case BCM2835_AUDIO_WRITE:
108 ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
112 LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
118 int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream)
120 struct bcm2835_audio_work *work;
122 work = kmalloc(sizeof(*work), GFP_ATOMIC);
123 /*--- Queue some work (item 1) ---*/
125 LOG_ERR(" .. Error: NULL work kmalloc\n");
128 INIT_WORK(&work->my_work, my_wq_function);
129 work->alsa_stream = alsa_stream;
130 work->cmd = BCM2835_AUDIO_START;
131 if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
138 int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream)
140 struct bcm2835_audio_work *work;
142 work = kmalloc(sizeof(*work), GFP_ATOMIC);
143 /*--- Queue some work (item 1) ---*/
145 LOG_ERR(" .. Error: NULL work kmalloc\n");
148 INIT_WORK(&work->my_work, my_wq_function);
149 work->alsa_stream = alsa_stream;
150 work->cmd = BCM2835_AUDIO_STOP;
151 if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
158 int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
159 unsigned int count, void *src)
161 struct bcm2835_audio_work *work;
163 work = kmalloc(sizeof(*work), GFP_ATOMIC);
164 /*--- Queue some work (item 1) ---*/
166 LOG_ERR(" .. Error: NULL work kmalloc\n");
169 INIT_WORK(&work->my_work, my_wq_function);
170 work->alsa_stream = alsa_stream;
171 work->cmd = BCM2835_AUDIO_WRITE;
174 if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
181 static void my_workqueue_quit(struct bcm2835_alsa_stream *alsa_stream)
183 flush_workqueue(alsa_stream->my_wq);
184 destroy_workqueue(alsa_stream->my_wq);
185 alsa_stream->my_wq = NULL;
188 static void audio_vchi_callback(void *param,
189 const VCHI_CALLBACK_REASON_T reason,
192 struct bcm2835_audio_instance *instance = param;
195 struct vc_audio_msg m;
197 if (reason != VCHI_CALLBACK_MSG_AVAILABLE)
201 LOG_ERR(" .. instance is null\n");
205 if (!instance->vchi_handle[0]) {
206 LOG_ERR(" .. instance->vchi_handle[0] is null\n");
210 status = vchi_msg_dequeue(instance->vchi_handle[0],
211 &m, sizeof(m), &msg_len, VCHI_FLAGS_NONE);
212 if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
213 LOG_DBG(" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
214 instance, m.u.result.success);
215 instance->result = m.u.result.success;
216 complete(&instance->msg_avail_comp);
217 } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
218 struct bcm2835_alsa_stream *alsa_stream = instance->alsa_stream;
220 LOG_DBG(" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
221 instance, m.u.complete.count);
222 if (m.u.complete.cookie1 != BCM2835_AUDIO_WRITE_COOKIE1 ||
223 m.u.complete.cookie2 != BCM2835_AUDIO_WRITE_COOKIE2)
224 LOG_ERR(" .. response is corrupt\n");
225 else if (alsa_stream) {
226 atomic_add(m.u.complete.count,
227 &alsa_stream->retrieved);
228 bcm2835_playback_fifo(alsa_stream);
230 LOG_ERR(" .. unexpected alsa_stream=%p\n",
234 LOG_ERR(" .. unexpected m.type=%d\n", m.type);
238 static struct bcm2835_audio_instance *
239 vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
240 VCHI_CONNECTION_T **vchi_connections,
241 unsigned int num_connections)
244 struct bcm2835_audio_instance *instance;
248 LOG_DBG("%s: start", __func__);
250 if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
251 LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
252 __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
254 return ERR_PTR(-EINVAL);
256 /* Allocate memory for this instance */
257 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
259 return ERR_PTR(-ENOMEM);
261 instance->num_connections = num_connections;
263 /* Create a lock for exclusive, serialized VCHI connection access */
264 mutex_init(&instance->vchi_mutex);
265 /* Open the VCHI service connections */
266 for (i = 0; i < num_connections; i++) {
267 SERVICE_CREATION_T params = {
268 .version = VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
269 .service_id = VC_AUDIO_SERVER_NAME,
270 .connection = vchi_connections[i],
273 .callback = audio_vchi_callback,
274 .callback_param = instance,
275 .want_unaligned_bulk_rx = 1, //TODO: remove VCOS_FALSE
276 .want_unaligned_bulk_tx = 1, //TODO: remove VCOS_FALSE
280 LOG_DBG("%s: about to open %i\n", __func__, i);
281 status = vchi_service_open(vchi_instance, ¶ms,
282 &instance->vchi_handle[i]);
284 LOG_DBG("%s: opened %i: %p=%d\n", __func__, i, instance->vchi_handle[i], status);
286 LOG_ERR("%s: failed to open VCHI service connection (status=%d)\n",
289 goto err_close_services;
291 /* Finished with the service for now */
292 vchi_service_release(instance->vchi_handle[i]);
295 LOG_DBG("%s: okay\n", __func__);
299 for (i = 0; i < instance->num_connections; i++) {
300 LOG_ERR("%s: closing %i: %p\n", __func__, i, instance->vchi_handle[i]);
301 if (instance->vchi_handle[i])
302 vchi_service_close(instance->vchi_handle[i]);
306 LOG_ERR("%s: error\n", __func__);
311 static int vc_vchi_audio_deinit(struct bcm2835_audio_instance *instance)
316 LOG_ERR("%s: invalid handle %p\n", __func__, instance);
321 LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
322 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
323 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
324 instance->num_connections);
328 /* Close all VCHI service connections */
329 for (i = 0; i < instance->num_connections; i++) {
332 LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
333 vchi_service_use(instance->vchi_handle[i]);
335 status = vchi_service_close(instance->vchi_handle[i]);
337 LOG_DBG("%s: failed to close VCHI service connection (status=%d)\n",
342 mutex_unlock(&instance->vchi_mutex);
349 int bcm2835_new_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx)
353 /* Initialize and create a VCHI connection */
354 ret = vchi_initialise(&vchi_ctx->vchi_instance);
356 LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
362 ret = vchi_connect(NULL, 0, vchi_ctx->vchi_instance);
364 LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
367 kfree(vchi_ctx->vchi_instance);
368 vchi_ctx->vchi_instance = NULL;
376 void bcm2835_free_vchi_ctx(struct bcm2835_vchi_ctx *vchi_ctx)
378 /* Close the VCHI connection - it will also free vchi_instance */
379 WARN_ON(vchi_disconnect(vchi_ctx->vchi_instance));
381 vchi_ctx->vchi_instance = NULL;
384 static int bcm2835_audio_open_connection(struct bcm2835_alsa_stream *alsa_stream)
386 struct bcm2835_audio_instance *instance =
387 (struct bcm2835_audio_instance *)alsa_stream->instance;
388 struct bcm2835_vchi_ctx *vhci_ctx = alsa_stream->chip->vchi_ctx;
390 LOG_INFO("%s: start\n", __func__);
393 LOG_ERR("%s: VCHI instance already open (%p)\n",
395 instance->alsa_stream = alsa_stream;
396 alsa_stream->instance = instance;
400 /* Initialize an instance of the audio service */
401 instance = vc_vchi_audio_init(vhci_ctx->vchi_instance,
402 &vhci_ctx->vchi_connection, 1);
404 if (IS_ERR(instance)) {
405 LOG_ERR("%s: failed to initialize audio service\n", __func__);
407 /* vchi_instance is retained for use the next time. */
408 return PTR_ERR(instance);
411 instance->alsa_stream = alsa_stream;
412 alsa_stream->instance = instance;
414 LOG_DBG(" success !\n");
419 int bcm2835_audio_open(struct bcm2835_alsa_stream *alsa_stream)
421 struct bcm2835_audio_instance *instance;
422 struct vc_audio_msg m;
426 alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
427 if (!alsa_stream->my_wq)
430 ret = bcm2835_audio_open_connection(alsa_stream);
434 instance = alsa_stream->instance;
435 LOG_DBG(" instance (%p)\n", instance);
437 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
438 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n", instance->num_connections);
442 vchi_service_use(instance->vchi_handle[0]);
444 m.type = VC_AUDIO_MSG_TYPE_OPEN;
446 /* Send the message to the videocore */
447 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
451 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
461 vchi_service_release(instance->vchi_handle[0]);
462 mutex_unlock(&instance->vchi_mutex);
466 destroy_workqueue(alsa_stream->my_wq);
471 static int bcm2835_audio_set_ctls_chan(struct bcm2835_alsa_stream *alsa_stream,
472 struct bcm2835_chip *chip)
474 struct vc_audio_msg m;
475 struct bcm2835_audio_instance *instance = alsa_stream->instance;
479 LOG_INFO(" Setting ALSA dest(%d), volume(%d)\n",
480 chip->dest, chip->volume);
482 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
483 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
484 instance->num_connections);
487 vchi_service_use(instance->vchi_handle[0]);
489 instance->result = -1;
491 m.type = VC_AUDIO_MSG_TYPE_CONTROL;
492 m.u.control.dest = chip->dest;
493 m.u.control.volume = chip->volume;
495 /* Create the message available completion */
496 init_completion(&instance->msg_avail_comp);
498 /* Send the message to the videocore */
499 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
503 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
510 /* We are expecting a reply from the videocore */
511 wait_for_completion(&instance->msg_avail_comp);
513 if (instance->result) {
514 LOG_ERR("%s: result=%d\n", __func__, instance->result);
523 vchi_service_release(instance->vchi_handle[0]);
524 mutex_unlock(&instance->vchi_mutex);
529 int bcm2835_audio_set_ctls(struct bcm2835_chip *chip)
534 LOG_DBG(" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
536 /* change ctls for all substreams */
537 for (i = 0; i < MAX_SUBSTREAMS; i++) {
538 if (chip->avail_substreams & (1 << i)) {
539 if (!chip->alsa_stream[i]) {
540 LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
542 } else if (bcm2835_audio_set_ctls_chan(chip->alsa_stream[i], chip) != 0) {
543 LOG_ERR("Couldn't set the controls for stream %d\n", i);
546 LOG_DBG(" Controls set for stream %d\n", i);
553 int bcm2835_audio_set_params(struct bcm2835_alsa_stream *alsa_stream,
554 unsigned int channels, unsigned int samplerate,
557 struct vc_audio_msg m;
558 struct bcm2835_audio_instance *instance = alsa_stream->instance;
562 LOG_INFO(" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
563 channels, samplerate, bps);
565 /* resend ctls - alsa_stream may not have been open when first send */
566 ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
568 LOG_ERR(" Alsa controls not supported\n");
572 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
573 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n", instance->num_connections);
576 vchi_service_use(instance->vchi_handle[0]);
578 instance->result = -1;
580 m.type = VC_AUDIO_MSG_TYPE_CONFIG;
581 m.u.config.channels = channels;
582 m.u.config.samplerate = samplerate;
583 m.u.config.bps = bps;
585 /* Create the message available completion */
586 init_completion(&instance->msg_avail_comp);
588 /* Send the message to the videocore */
589 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
593 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
600 /* We are expecting a reply from the videocore */
601 wait_for_completion(&instance->msg_avail_comp);
603 if (instance->result) {
604 LOG_ERR("%s: result=%d", __func__, instance->result);
613 vchi_service_release(instance->vchi_handle[0]);
614 mutex_unlock(&instance->vchi_mutex);
619 int bcm2835_audio_setup(struct bcm2835_alsa_stream *alsa_stream)
625 static int bcm2835_audio_start_worker(struct bcm2835_alsa_stream *alsa_stream)
627 struct vc_audio_msg m;
628 struct bcm2835_audio_instance *instance = alsa_stream->instance;
632 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
633 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
634 instance->num_connections);
637 vchi_service_use(instance->vchi_handle[0]);
639 m.type = VC_AUDIO_MSG_TYPE_START;
641 /* Send the message to the videocore */
642 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
646 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
656 vchi_service_release(instance->vchi_handle[0]);
657 mutex_unlock(&instance->vchi_mutex);
661 static int bcm2835_audio_stop_worker(struct bcm2835_alsa_stream *alsa_stream)
663 struct vc_audio_msg m;
664 struct bcm2835_audio_instance *instance = alsa_stream->instance;
668 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
669 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
670 instance->num_connections);
673 vchi_service_use(instance->vchi_handle[0]);
675 m.type = VC_AUDIO_MSG_TYPE_STOP;
676 m.u.stop.draining = alsa_stream->draining;
678 /* Send the message to the videocore */
679 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
683 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
693 vchi_service_release(instance->vchi_handle[0]);
694 mutex_unlock(&instance->vchi_mutex);
698 int bcm2835_audio_close(struct bcm2835_alsa_stream *alsa_stream)
700 struct vc_audio_msg m;
701 struct bcm2835_audio_instance *instance = alsa_stream->instance;
705 my_workqueue_quit(alsa_stream);
707 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
708 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
709 instance->num_connections);
712 vchi_service_use(instance->vchi_handle[0]);
714 m.type = VC_AUDIO_MSG_TYPE_CLOSE;
716 /* Create the message available completion */
717 init_completion(&instance->msg_avail_comp);
719 /* Send the message to the videocore */
720 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
724 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
730 /* We are expecting a reply from the videocore */
731 wait_for_completion(&instance->msg_avail_comp);
733 if (instance->result) {
734 LOG_ERR("%s: failed result (result=%d)\n",
735 __func__, instance->result);
744 vchi_service_release(instance->vchi_handle[0]);
745 mutex_unlock(&instance->vchi_mutex);
747 /* Stop the audio service */
748 vc_vchi_audio_deinit(instance);
749 alsa_stream->instance = NULL;
754 static int bcm2835_audio_write_worker(struct bcm2835_alsa_stream *alsa_stream,
755 unsigned int count, void *src)
757 struct vc_audio_msg m;
758 struct bcm2835_audio_instance *instance = alsa_stream->instance;
762 LOG_INFO(" Writing %d bytes from %p\n", count, src);
764 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
765 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
766 instance->num_connections);
769 vchi_service_use(instance->vchi_handle[0]);
771 if (instance->peer_version == 0 &&
772 vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0)
773 LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
775 m.type = VC_AUDIO_MSG_TYPE_WRITE;
776 m.u.write.count = count;
777 // old version uses bulk, new version uses control
778 m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0 : 4000;
779 m.u.write.cookie1 = BCM2835_AUDIO_WRITE_COOKIE1;
780 m.u.write.cookie2 = BCM2835_AUDIO_WRITE_COOKIE2;
781 m.u.write.silence = src == NULL;
783 /* Send the message to the videocore */
784 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
788 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
794 if (!m.u.write.silence) {
795 if (!m.u.write.max_packet) {
796 /* Send the message to the videocore */
797 status = vchi_bulk_queue_transmit(instance->vchi_handle[0],
799 0 * VCHI_FLAGS_BLOCK_UNTIL_QUEUED
801 1 * VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
805 int bytes = min_t(int, m.u.write.max_packet, count);
807 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
809 src = (char *)src + bytes;
814 LOG_ERR("%s: failed on vchi_bulk_queue_transmit (status=%d)\n",
824 vchi_service_release(instance->vchi_handle[0]);
825 mutex_unlock(&instance->vchi_mutex);
830 * Returns all buffers from arm->vc
832 void bcm2835_audio_flush_buffers(struct bcm2835_alsa_stream *alsa_stream)
837 * Forces VC to flush(drop) its filled playback buffers and
838 * return them the us. (VC->ARM)
840 void bcm2835_audio_flush_playback_buffers(struct bcm2835_alsa_stream *alsa_stream)
844 unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream)
846 unsigned int count = atomic_read(&alsa_stream->retrieved);
848 atomic_sub(count, &alsa_stream->retrieved);
852 module_param(force_bulk, bool, 0444);
853 MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");