1 /*****************************************************************************
2 * Copyright 2011 Broadcom Corporation. All rights reserved.
4 * Unless you and Broadcom execute a separate written software license
5 * agreement governing use of this software, this software is licensed to you
6 * under the terms of the GNU General Public License version 2, available at
7 * http://www.broadcom.com/licenses/GPLv2.php (the "GPL").
9 * Notwithstanding the above, under no circumstances may you combine this
10 * software in any way with any other Broadcom software provided under a
11 * license other than the GPL, without Broadcom's express prior written
13 *****************************************************************************/
15 #include <linux/device.h>
16 #include <sound/core.h>
17 #include <sound/initval.h>
18 #include <sound/pcm.h>
20 #include <linux/interrupt.h>
22 #include <linux/file.h>
24 #include <linux/syscalls.h>
25 #include <linux/uaccess.h>
26 #include <linux/slab.h>
27 #include <linux/delay.h>
28 #include <linux/atomic.h>
29 #include <linux/module.h>
30 #include <linux/completion.h>
34 /* ---- Include Files -------------------------------------------------------- */
36 #include "interface/vchi/vchi.h"
37 #include "vc_vchi_audioserv_defs.h"
39 /* ---- Private Constants and Types ------------------------------------------ */
41 #define BCM2835_AUDIO_STOP 0
42 #define BCM2835_AUDIO_START 1
43 #define BCM2835_AUDIO_WRITE 2
45 /* Logging macros (for remapping to other logging mechanisms, i.e., printf) */
46 #ifdef AUDIO_DEBUG_ENABLE
47 #define LOG_ERR(fmt, arg...) pr_err("%s:%d " fmt, __func__, __LINE__, ##arg)
48 #define LOG_WARN(fmt, arg...) pr_info("%s:%d " fmt, __func__, __LINE__, ##arg)
49 #define LOG_INFO(fmt, arg...) pr_info("%s:%d " fmt, __func__, __LINE__, ##arg)
50 #define LOG_DBG(fmt, arg...) pr_info("%s:%d " fmt, __func__, __LINE__, ##arg)
52 #define LOG_ERR(fmt, arg...) pr_err("%s:%d " fmt, __func__, __LINE__, ##arg)
53 #define LOG_WARN(fmt, arg...) no_printk(fmt, ##arg)
54 #define LOG_INFO(fmt, arg...) no_printk(fmt, ##arg)
55 #define LOG_DBG(fmt, arg...) no_printk(fmt, ##arg)
58 struct bcm2835_audio_instance {
59 unsigned int num_connections;
60 VCHI_SERVICE_HANDLE_T vchi_handle[VCHI_MAX_NUM_CONNECTIONS];
61 struct completion msg_avail_comp;
62 struct mutex vchi_mutex;
63 struct bcm2835_alsa_stream *alsa_stream;
68 static bool force_bulk;
70 /* ---- Private Variables ---------------------------------------------------- */
72 /* ---- Private Function Prototypes ------------------------------------------ */
74 /* ---- Private Functions ---------------------------------------------------- */
76 static int bcm2835_audio_stop_worker(struct bcm2835_alsa_stream *alsa_stream);
77 static int bcm2835_audio_start_worker(struct bcm2835_alsa_stream *alsa_stream);
78 static int bcm2835_audio_write_worker(struct bcm2835_alsa_stream *alsa_stream,
79 unsigned int count, void *src);
81 // Routine to send a message across a service
84 bcm2835_vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
88 return vchi_queue_kernel_message(handle,
93 static const u32 BCM2835_AUDIO_WRITE_COOKIE1 = ('B' << 24 | 'C' << 16 |
95 static const u32 BCM2835_AUDIO_WRITE_COOKIE2 = ('D' << 24 | 'A' << 16 |
98 struct bcm2835_audio_work {
99 struct work_struct my_work;
100 struct bcm2835_alsa_stream *alsa_stream;
106 static void my_wq_function(struct work_struct *work)
108 struct bcm2835_audio_work *w =
109 container_of(work, struct bcm2835_audio_work, my_work);
113 case BCM2835_AUDIO_START:
114 ret = bcm2835_audio_start_worker(w->alsa_stream);
116 case BCM2835_AUDIO_STOP:
117 ret = bcm2835_audio_stop_worker(w->alsa_stream);
119 case BCM2835_AUDIO_WRITE:
120 ret = bcm2835_audio_write_worker(w->alsa_stream, w->count,
124 LOG_ERR(" Unexpected work: %p:%d\n", w->alsa_stream, w->cmd);
130 int bcm2835_audio_start(struct bcm2835_alsa_stream *alsa_stream)
132 if (alsa_stream->my_wq) {
133 struct bcm2835_audio_work *work;
135 work = kmalloc(sizeof(*work), GFP_ATOMIC);
136 /*--- Queue some work (item 1) ---*/
138 LOG_ERR(" .. Error: NULL work kmalloc\n");
141 INIT_WORK(&work->my_work, my_wq_function);
142 work->alsa_stream = alsa_stream;
143 work->cmd = BCM2835_AUDIO_START;
144 if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
152 int bcm2835_audio_stop(struct bcm2835_alsa_stream *alsa_stream)
154 if (alsa_stream->my_wq) {
155 struct bcm2835_audio_work *work;
157 work = kmalloc(sizeof(*work), GFP_ATOMIC);
158 /*--- Queue some work (item 1) ---*/
160 LOG_ERR(" .. Error: NULL work kmalloc\n");
163 INIT_WORK(&work->my_work, my_wq_function);
164 work->alsa_stream = alsa_stream;
165 work->cmd = BCM2835_AUDIO_STOP;
166 if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
174 int bcm2835_audio_write(struct bcm2835_alsa_stream *alsa_stream,
175 unsigned int count, void *src)
177 if (alsa_stream->my_wq) {
178 struct bcm2835_audio_work *work;
180 work = kmalloc(sizeof(*work), GFP_ATOMIC);
181 /*--- Queue some work (item 1) ---*/
183 LOG_ERR(" .. Error: NULL work kmalloc\n");
186 INIT_WORK(&work->my_work, my_wq_function);
187 work->alsa_stream = alsa_stream;
188 work->cmd = BCM2835_AUDIO_WRITE;
191 if (!queue_work(alsa_stream->my_wq, &work->my_work)) {
199 static void my_workqueue_init(struct bcm2835_alsa_stream *alsa_stream)
201 alsa_stream->my_wq = alloc_workqueue("my_queue", WQ_HIGHPRI, 1);
204 static void my_workqueue_quit(struct bcm2835_alsa_stream *alsa_stream)
206 if (alsa_stream->my_wq) {
207 flush_workqueue(alsa_stream->my_wq);
208 destroy_workqueue(alsa_stream->my_wq);
209 alsa_stream->my_wq = NULL;
213 static void audio_vchi_callback(void *param,
214 const VCHI_CALLBACK_REASON_T reason,
217 struct bcm2835_audio_instance *instance = param;
220 struct vc_audio_msg m;
222 if (reason != VCHI_CALLBACK_MSG_AVAILABLE)
226 LOG_ERR(" .. instance is null\n");
230 if (!instance->vchi_handle[0]) {
231 LOG_ERR(" .. instance->vchi_handle[0] is null\n");
235 status = vchi_msg_dequeue(instance->vchi_handle[0],
236 &m, sizeof(m), &msg_len, VCHI_FLAGS_NONE);
237 if (m.type == VC_AUDIO_MSG_TYPE_RESULT) {
238 LOG_DBG(" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_RESULT, success=%d\n",
239 instance, m.u.result.success);
240 instance->result = m.u.result.success;
241 complete(&instance->msg_avail_comp);
242 } else if (m.type == VC_AUDIO_MSG_TYPE_COMPLETE) {
243 struct bcm2835_alsa_stream *alsa_stream = instance->alsa_stream;
245 LOG_DBG(" .. instance=%p, m.type=VC_AUDIO_MSG_TYPE_COMPLETE, complete=%d\n",
246 instance, m.u.complete.count);
247 if (m.u.complete.cookie1 != BCM2835_AUDIO_WRITE_COOKIE1 ||
248 m.u.complete.cookie2 != BCM2835_AUDIO_WRITE_COOKIE2)
249 LOG_ERR(" .. response is corrupt\n");
250 else if (alsa_stream) {
251 atomic_add(m.u.complete.count,
252 &alsa_stream->retrieved);
253 bcm2835_playback_fifo(alsa_stream);
255 LOG_ERR(" .. unexpected alsa_stream=%p\n",
259 LOG_ERR(" .. unexpected m.type=%d\n", m.type);
263 static struct bcm2835_audio_instance *
264 vc_vchi_audio_init(VCHI_INSTANCE_T vchi_instance,
265 VCHI_CONNECTION_T **vchi_connections,
266 unsigned int num_connections)
269 struct bcm2835_audio_instance *instance;
273 LOG_DBG("%s: start", __func__);
275 if (num_connections > VCHI_MAX_NUM_CONNECTIONS) {
276 LOG_ERR("%s: unsupported number of connections %u (max=%u)\n",
277 __func__, num_connections, VCHI_MAX_NUM_CONNECTIONS);
279 return ERR_PTR(-EINVAL);
281 /* Allocate memory for this instance */
282 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
284 return ERR_PTR(-ENOMEM);
286 instance->num_connections = num_connections;
288 /* Create a lock for exclusive, serialized VCHI connection access */
289 mutex_init(&instance->vchi_mutex);
290 /* Open the VCHI service connections */
291 for (i = 0; i < num_connections; i++) {
292 SERVICE_CREATION_T params = {
293 .version = VCHI_VERSION_EX(VC_AUDIOSERV_VER, VC_AUDIOSERV_MIN_VER),
294 .service_id = VC_AUDIO_SERVER_NAME,
295 .connection = vchi_connections[i],
298 .callback = audio_vchi_callback,
299 .callback_param = instance,
300 .want_unaligned_bulk_rx = 1, //TODO: remove VCOS_FALSE
301 .want_unaligned_bulk_tx = 1, //TODO: remove VCOS_FALSE
305 LOG_DBG("%s: about to open %i\n", __func__, i);
306 status = vchi_service_open(vchi_instance, ¶ms,
307 &instance->vchi_handle[i]);
309 LOG_DBG("%s: opened %i: %p=%d\n", __func__, i, instance->vchi_handle[i], status);
311 LOG_ERR("%s: failed to open VCHI service connection (status=%d)\n",
314 goto err_close_services;
316 /* Finished with the service for now */
317 vchi_service_release(instance->vchi_handle[i]);
320 LOG_DBG("%s: okay\n", __func__);
324 for (i = 0; i < instance->num_connections; i++) {
325 LOG_ERR("%s: closing %i: %p\n", __func__, i, instance->vchi_handle[i]);
326 if (instance->vchi_handle[i])
327 vchi_service_close(instance->vchi_handle[i]);
331 LOG_ERR("%s: error\n", __func__);
336 static int vc_vchi_audio_deinit(struct bcm2835_audio_instance *instance)
342 LOG_ERR("%s: invalid handle %p\n", __func__, instance);
347 LOG_DBG(" .. about to lock (%d)\n", instance->num_connections);
348 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
349 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
350 instance->num_connections);
354 /* Close all VCHI service connections */
355 for (i = 0; i < instance->num_connections; i++) {
358 LOG_DBG(" .. %i:closing %p\n", i, instance->vchi_handle[i]);
359 vchi_service_use(instance->vchi_handle[i]);
361 status = vchi_service_close(instance->vchi_handle[i]);
363 LOG_DBG("%s: failed to close VCHI service connection (status=%d)\n",
368 mutex_unlock(&instance->vchi_mutex);
376 static int bcm2835_audio_open_connection(struct bcm2835_alsa_stream *alsa_stream)
378 static VCHI_INSTANCE_T vchi_instance;
379 static VCHI_CONNECTION_T *vchi_connection;
381 struct bcm2835_audio_instance *instance =
382 (struct bcm2835_audio_instance *)alsa_stream->instance;
386 LOG_INFO("%s: start\n", __func__);
389 LOG_ERR("%s: VCHI instance already open (%p)\n",
391 instance->alsa_stream = alsa_stream;
392 alsa_stream->instance = instance;
396 /* Initialize and create a VCHI connection */
398 ret = vchi_initialise(&vchi_instance);
400 LOG_ERR("%s: failed to initialise VCHI instance (ret=%d)\n",
405 ret = vchi_connect(NULL, 0, vchi_instance);
407 LOG_ERR("%s: failed to connect VCHI instance (ret=%d)\n",
410 kfree(vchi_instance);
416 /* Initialize an instance of the audio service */
417 instance = vc_vchi_audio_init(vchi_instance, &vchi_connection, 1);
419 if (IS_ERR(instance)) {
420 LOG_ERR("%s: failed to initialize audio service\n", __func__);
422 /* vchi_instance is retained for use the next time. */
423 return PTR_ERR(instance);
426 instance->alsa_stream = alsa_stream;
427 alsa_stream->instance = instance;
429 LOG_DBG(" success !\n");
434 int bcm2835_audio_open(struct bcm2835_alsa_stream *alsa_stream)
436 struct bcm2835_audio_instance *instance;
437 struct vc_audio_msg m;
442 my_workqueue_init(alsa_stream);
444 ret = bcm2835_audio_open_connection(alsa_stream);
448 instance = alsa_stream->instance;
449 LOG_DBG(" instance (%p)\n", instance);
451 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
452 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n", instance->num_connections);
456 vchi_service_use(instance->vchi_handle[0]);
458 m.type = VC_AUDIO_MSG_TYPE_OPEN;
460 /* Send the message to the videocore */
461 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
465 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
475 vchi_service_release(instance->vchi_handle[0]);
476 mutex_unlock(&instance->vchi_mutex);
480 destroy_workqueue(alsa_stream->my_wq);
485 static int bcm2835_audio_set_ctls_chan(struct bcm2835_alsa_stream *alsa_stream,
486 struct bcm2835_chip *chip)
488 struct vc_audio_msg m;
489 struct bcm2835_audio_instance *instance = alsa_stream->instance;
494 LOG_INFO(" Setting ALSA dest(%d), volume(%d)\n",
495 chip->dest, chip->volume);
497 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
498 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
499 instance->num_connections);
502 vchi_service_use(instance->vchi_handle[0]);
504 instance->result = -1;
506 m.type = VC_AUDIO_MSG_TYPE_CONTROL;
507 m.u.control.dest = chip->dest;
508 m.u.control.volume = chip->volume;
510 /* Create the message available completion */
511 init_completion(&instance->msg_avail_comp);
513 /* Send the message to the videocore */
514 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
518 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
525 /* We are expecting a reply from the videocore */
526 wait_for_completion(&instance->msg_avail_comp);
528 if (instance->result) {
529 LOG_ERR("%s: result=%d\n", __func__, instance->result);
538 vchi_service_release(instance->vchi_handle[0]);
539 mutex_unlock(&instance->vchi_mutex);
544 int bcm2835_audio_set_ctls(struct bcm2835_chip *chip)
549 LOG_DBG(" Setting ALSA dest(%d), volume(%d)\n", chip->dest, chip->volume);
551 /* change ctls for all substreams */
552 for (i = 0; i < MAX_SUBSTREAMS; i++) {
553 if (chip->avail_substreams & (1 << i)) {
554 if (!chip->alsa_stream[i]) {
555 LOG_DBG(" No ALSA stream available?! %i:%p (%x)\n", i, chip->alsa_stream[i], chip->avail_substreams);
557 } else if (bcm2835_audio_set_ctls_chan(chip->alsa_stream[i], chip) != 0) {
558 LOG_ERR("Couldn't set the controls for stream %d\n", i);
561 LOG_DBG(" Controls set for stream %d\n", i);
568 int bcm2835_audio_set_params(struct bcm2835_alsa_stream *alsa_stream,
569 unsigned int channels, unsigned int samplerate,
572 struct vc_audio_msg m;
573 struct bcm2835_audio_instance *instance = alsa_stream->instance;
578 LOG_INFO(" Setting ALSA channels(%d), samplerate(%d), bits-per-sample(%d)\n",
579 channels, samplerate, bps);
581 /* resend ctls - alsa_stream may not have been open when first send */
582 ret = bcm2835_audio_set_ctls_chan(alsa_stream, alsa_stream->chip);
584 LOG_ERR(" Alsa controls not supported\n");
588 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
589 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n", instance->num_connections);
592 vchi_service_use(instance->vchi_handle[0]);
594 instance->result = -1;
596 m.type = VC_AUDIO_MSG_TYPE_CONFIG;
597 m.u.config.channels = channels;
598 m.u.config.samplerate = samplerate;
599 m.u.config.bps = bps;
601 /* Create the message available completion */
602 init_completion(&instance->msg_avail_comp);
604 /* Send the message to the videocore */
605 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
609 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
616 /* We are expecting a reply from the videocore */
617 wait_for_completion(&instance->msg_avail_comp);
619 if (instance->result) {
620 LOG_ERR("%s: result=%d", __func__, instance->result);
629 vchi_service_release(instance->vchi_handle[0]);
630 mutex_unlock(&instance->vchi_mutex);
635 int bcm2835_audio_setup(struct bcm2835_alsa_stream *alsa_stream)
642 static int bcm2835_audio_start_worker(struct bcm2835_alsa_stream *alsa_stream)
644 struct vc_audio_msg m;
645 struct bcm2835_audio_instance *instance = alsa_stream->instance;
650 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
651 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
652 instance->num_connections);
655 vchi_service_use(instance->vchi_handle[0]);
657 m.type = VC_AUDIO_MSG_TYPE_START;
659 /* Send the message to the videocore */
660 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
664 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
674 vchi_service_release(instance->vchi_handle[0]);
675 mutex_unlock(&instance->vchi_mutex);
679 static int bcm2835_audio_stop_worker(struct bcm2835_alsa_stream *alsa_stream)
681 struct vc_audio_msg m;
682 struct bcm2835_audio_instance *instance = alsa_stream->instance;
687 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
688 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
689 instance->num_connections);
692 vchi_service_use(instance->vchi_handle[0]);
694 m.type = VC_AUDIO_MSG_TYPE_STOP;
695 m.u.stop.draining = alsa_stream->draining;
697 /* Send the message to the videocore */
698 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
702 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
712 vchi_service_release(instance->vchi_handle[0]);
713 mutex_unlock(&instance->vchi_mutex);
717 int bcm2835_audio_close(struct bcm2835_alsa_stream *alsa_stream)
719 struct vc_audio_msg m;
720 struct bcm2835_audio_instance *instance = alsa_stream->instance;
725 my_workqueue_quit(alsa_stream);
727 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
728 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
729 instance->num_connections);
732 vchi_service_use(instance->vchi_handle[0]);
734 m.type = VC_AUDIO_MSG_TYPE_CLOSE;
736 /* Create the message available completion */
737 init_completion(&instance->msg_avail_comp);
739 /* Send the message to the videocore */
740 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
744 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
750 /* We are expecting a reply from the videocore */
751 wait_for_completion(&instance->msg_avail_comp);
753 if (instance->result) {
754 LOG_ERR("%s: failed result (result=%d)\n",
755 __func__, instance->result);
764 vchi_service_release(instance->vchi_handle[0]);
765 mutex_unlock(&instance->vchi_mutex);
767 /* Stop the audio service */
768 vc_vchi_audio_deinit(instance);
769 alsa_stream->instance = NULL;
774 static int bcm2835_audio_write_worker(struct bcm2835_alsa_stream *alsa_stream,
775 unsigned int count, void *src)
777 struct vc_audio_msg m;
778 struct bcm2835_audio_instance *instance = alsa_stream->instance;
783 LOG_INFO(" Writing %d bytes from %p\n", count, src);
785 if (mutex_lock_interruptible(&instance->vchi_mutex)) {
786 LOG_DBG("Interrupted whilst waiting for lock on (%d)\n",
787 instance->num_connections);
790 vchi_service_use(instance->vchi_handle[0]);
792 if (instance->peer_version == 0 &&
793 vchi_get_peer_version(instance->vchi_handle[0], &instance->peer_version) == 0)
794 LOG_DBG("%s: client version %d connected\n", __func__, instance->peer_version);
796 m.type = VC_AUDIO_MSG_TYPE_WRITE;
797 m.u.write.count = count;
798 // old version uses bulk, new version uses control
799 m.u.write.max_packet = instance->peer_version < 2 || force_bulk ? 0 : 4000;
800 m.u.write.cookie1 = BCM2835_AUDIO_WRITE_COOKIE1;
801 m.u.write.cookie2 = BCM2835_AUDIO_WRITE_COOKIE2;
802 m.u.write.silence = src == NULL;
804 /* Send the message to the videocore */
805 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
809 LOG_ERR("%s: failed on vchi_msg_queue (status=%d)\n",
815 if (!m.u.write.silence) {
816 if (!m.u.write.max_packet) {
817 /* Send the message to the videocore */
818 status = vchi_bulk_queue_transmit(instance->vchi_handle[0],
820 0 * VCHI_FLAGS_BLOCK_UNTIL_QUEUED
822 1 * VCHI_FLAGS_BLOCK_UNTIL_DATA_READ,
826 int bytes = min_t(int, m.u.write.max_packet, count);
828 status = bcm2835_vchi_msg_queue(instance->vchi_handle[0],
830 src = (char *)src + bytes;
835 LOG_ERR("%s: failed on vchi_bulk_queue_transmit (status=%d)\n",
845 vchi_service_release(instance->vchi_handle[0]);
846 mutex_unlock(&instance->vchi_mutex);
851 * Returns all buffers from arm->vc
853 void bcm2835_audio_flush_buffers(struct bcm2835_alsa_stream *alsa_stream)
858 * Forces VC to flush(drop) its filled playback buffers and
859 * return them the us. (VC->ARM)
861 void bcm2835_audio_flush_playback_buffers(struct bcm2835_alsa_stream *alsa_stream)
865 unsigned int bcm2835_audio_retrieve_buffers(struct bcm2835_alsa_stream *alsa_stream)
867 unsigned int count = atomic_read(&alsa_stream->retrieved);
869 atomic_sub(count, &alsa_stream->retrieved);
873 module_param(force_bulk, bool, 0444);
874 MODULE_PARM_DESC(force_bulk, "Force use of vchiq bulk for audio");