1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2010-2012 Broadcom. All rights reserved. */
4 #include "vchiq_connected.h"
5 #include "vchiq_core.h"
6 #include <linux/module.h>
7 #include <linux/mutex.h>
9 #define MAX_CALLBACKS 10
11 static int g_connected;
12 static int g_num_deferred_callbacks;
13 static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
14 static int g_once_init;
15 static struct mutex g_connected_mutex;
17 /****************************************************************************
19 * Function to initialize our lock.
21 ***************************************************************************/
23 static void connected_init(void)
26 mutex_init(&g_connected_mutex);
31 /****************************************************************************
33 * This function is used to defer initialization until the vchiq stack is
34 * initialized. If the stack is already initialized, then the callback will
35 * be made immediately, otherwise it will be deferred until
36 * vchiq_call_connected_callbacks is called.
38 ***************************************************************************/
40 void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
44 if (mutex_lock_killable(&g_connected_mutex))
48 /* We're already connected. Call the callback immediately. */
52 if (g_num_deferred_callbacks >= MAX_CALLBACKS)
53 vchiq_log_error(vchiq_core_log_level,
54 "There already %d callback registered - "
55 "please increase MAX_CALLBACKS",
56 g_num_deferred_callbacks);
58 g_deferred_callback[g_num_deferred_callbacks] =
60 g_num_deferred_callbacks++;
63 mutex_unlock(&g_connected_mutex);
66 /****************************************************************************
68 * This function is called by the vchiq stack once it has been connected to
69 * the videocore and clients can start to use the stack.
71 ***************************************************************************/
73 void vchiq_call_connected_callbacks(void)
79 if (mutex_lock_killable(&g_connected_mutex))
82 for (i = 0; i < g_num_deferred_callbacks; i++)
83 g_deferred_callback[i]();
85 g_num_deferred_callbacks = 0;
87 mutex_unlock(&g_connected_mutex);
89 EXPORT_SYMBOL(vchiq_add_connected_callback);