1 1.1 jmcneill /** 2 1.1 jmcneill * Copyright (c) 2010-2012 Broadcom. All rights reserved. 3 1.1 jmcneill * 4 1.1 jmcneill * Redistribution and use in source and binary forms, with or without 5 1.1 jmcneill * modification, are permitted provided that the following conditions 6 1.1 jmcneill * are met: 7 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright 8 1.1 jmcneill * notice, this list of conditions, and the following disclaimer, 9 1.1 jmcneill * without modification. 10 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright 11 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the 12 1.1 jmcneill * documentation and/or other materials provided with the distribution. 13 1.1 jmcneill * 3. The names of the above-listed copyright holders may not be used 14 1.1 jmcneill * to endorse or promote products derived from this software without 15 1.1 jmcneill * specific prior written permission. 16 1.1 jmcneill * 17 1.1 jmcneill * ALTERNATIVELY, this software may be distributed under the terms of the 18 1.1 jmcneill * GNU General Public License ("GPL") version 2, as published by the Free 19 1.1 jmcneill * Software Foundation. 20 1.1 jmcneill * 21 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 22 1.1 jmcneill * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 23 1.1 jmcneill * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 25 1.1 jmcneill * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 26 1.1 jmcneill * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 1.1 jmcneill * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 28 1.1 jmcneill * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 1.1 jmcneill * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 1.1 jmcneill * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 1.1 jmcneill * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.1 jmcneill */ 33 1.1 jmcneill 34 1.1 jmcneill #include "vchiq_connected.h" 35 1.1 jmcneill #include "vchiq_core.h" 36 1.1 jmcneill 37 1.1 jmcneill #define MAX_CALLBACKS 10 38 1.1 jmcneill 39 1.1 jmcneill static int g_connected; 40 1.1 jmcneill static int g_num_deferred_callbacks; 41 1.1 jmcneill static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS]; 42 1.1 jmcneill static int g_once_init; 43 1.1 jmcneill static struct mutex g_connected_mutex; 44 1.1 jmcneill 45 1.1 jmcneill /**************************************************************************** 46 1.1 jmcneill * 47 1.1 jmcneill * Function to initialize our lock. 48 1.1 jmcneill * 49 1.1 jmcneill ***************************************************************************/ 50 1.1 jmcneill 51 1.1 jmcneill static void connected_init(void) 52 1.1 jmcneill { 53 1.1 jmcneill if (!g_once_init) { 54 1.1 jmcneill lmutex_init(&g_connected_mutex); 55 1.1 jmcneill g_once_init = 1; 56 1.1 jmcneill } 57 1.1 jmcneill } 58 1.1 jmcneill 59 1.1 jmcneill /**************************************************************************** 60 1.1 jmcneill * 61 1.1 jmcneill * This function is used to defer initialization until the vchiq stack is 62 1.1 jmcneill * initialized. If the stack is already initialized, then the callback will 63 1.1 jmcneill * be made immediately, otherwise it will be deferred until 64 1.1 jmcneill * vchiq_call_connected_callbacks is called. 65 1.1 jmcneill * 66 1.1 jmcneill ***************************************************************************/ 67 1.1 jmcneill 68 1.1 jmcneill void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback) 69 1.1 jmcneill { 70 1.1 jmcneill connected_init(); 71 1.1 jmcneill 72 1.1 jmcneill if (lmutex_lock_interruptible(&g_connected_mutex) != 0) 73 1.1 jmcneill return; 74 1.1 jmcneill 75 1.1 jmcneill if (g_connected) 76 1.1 jmcneill /* We're already connected. Call the callback immediately. */ 77 1.1 jmcneill 78 1.1 jmcneill callback(); 79 1.1 jmcneill else { 80 1.1 jmcneill if (g_num_deferred_callbacks >= MAX_CALLBACKS) 81 1.1 jmcneill vchiq_log_error(vchiq_core_log_level, 82 1.1 jmcneill "There are already %d callbacks registered - " 83 1.1 jmcneill "please increase MAX_CALLBACKS", 84 1.1 jmcneill g_num_deferred_callbacks); 85 1.1 jmcneill else { 86 1.1 jmcneill g_deferred_callback[g_num_deferred_callbacks] = 87 1.1 jmcneill callback; 88 1.1 jmcneill g_num_deferred_callbacks++; 89 1.1 jmcneill } 90 1.1 jmcneill } 91 1.1 jmcneill lmutex_unlock(&g_connected_mutex); 92 1.1 jmcneill } 93 1.1 jmcneill 94 1.1 jmcneill /**************************************************************************** 95 1.1 jmcneill * 96 1.1 jmcneill * This function is called by the vchiq stack once it has been connected to 97 1.1 jmcneill * the videocore and clients can start to use the stack. 98 1.1 jmcneill * 99 1.1 jmcneill ***************************************************************************/ 100 1.1 jmcneill 101 1.1 jmcneill void vchiq_call_connected_callbacks(void) 102 1.1 jmcneill { 103 1.1 jmcneill int i; 104 1.1 jmcneill 105 1.1 jmcneill connected_init(); 106 1.1 jmcneill 107 1.1 jmcneill if (lmutex_lock_interruptible(&g_connected_mutex) != 0) 108 1.1 jmcneill return; 109 1.1 jmcneill 110 1.1 jmcneill for (i = 0; i < g_num_deferred_callbacks; i++) 111 1.1 jmcneill g_deferred_callback[i](); 112 1.1 jmcneill 113 1.1 jmcneill g_num_deferred_callbacks = 0; 114 1.1 jmcneill g_connected = 1; 115 1.1 jmcneill lmutex_unlock(&g_connected_mutex); 116 1.1 jmcneill } 117 1.1 jmcneill EXPORT_SYMBOL(vchiq_add_connected_callback); 118