A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita audio rust zig deno mpris rockbox mpd
at master 204 lines 5.0 kB view raw
1/*************************************************************************** 2 * __________ __ ___. 3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___ 4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / 5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < 6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ 7 * \/ \/ \/ \/ \/ 8 * $Id$ 9 * 10 * Copyright (C) 2002 by Alan Korr 11 * 12 * This program is free software; you can redistribute it and/or 13 * modify it under the terms of the GNU General Public License 14 * as published by the Free Software Foundation; either version 2 15 * of the License, or (at your option) any later version. 16 * 17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 18 * KIND, either express or implied. 19 * 20 ****************************************************************************/ 21#include "config.h" 22#include "system.h" 23#include <stdio.h> 24#include "kernel.h" 25#include "thread.h" 26#include "string.h" 27#include "file.h" 28 29#ifndef CPU_FREQ 30#define CPU_FREQ (-1) 31#endif 32 33long cpu_frequency SHAREDBSS_ATTR = CPU_FREQ; 34 35#ifdef HAVE_ADJUSTABLE_CPU_FREQ 36static int boost_counter SHAREDBSS_ATTR = 0; 37static bool cpu_idle SHAREDBSS_ATTR = false; 38 39int get_cpu_boost_counter(void) 40{ 41 return boost_counter; 42} 43#ifdef CPU_BOOST_LOGGING 44#define MAX_BOOST_LOG 64 45static char cpu_boost_calls[MAX_BOOST_LOG][MAX_PATH]; 46static int cpu_boost_first = 0; 47static int cpu_boost_calls_count = 0; 48static int cpu_boost_track_message = 0; 49int cpu_boost_log_getcount(void) 50{ 51 return cpu_boost_calls_count; 52} 53char * cpu_boost_log_getlog_first(void) 54{ 55 char *first; 56 cpu_boost_lock(); 57 58 first = NULL; 59 60 if (cpu_boost_calls_count) 61 { 62 cpu_boost_track_message = 1; 63 first = cpu_boost_calls[cpu_boost_first]; 64 } 65 66 cpu_boost_unlock(); 67 return first; 68} 69 70char * cpu_boost_log_getlog_next(void) 71{ 72 int message; 73 char *next; 74 75 cpu_boost_lock(); 76 77 message = (cpu_boost_track_message+cpu_boost_first)%MAX_BOOST_LOG; 78 next = NULL; 79 80 if (cpu_boost_track_message < cpu_boost_calls_count) 81 { 82 cpu_boost_track_message++; 83 next = cpu_boost_calls[message]; 84 } 85 86 cpu_boost_unlock(); 87 return next; 88} 89 90void cpu_boost_(bool on_off, char* location, int line) 91{ 92 int item = cpu_boost_calls_count; 93 if (!cpu_boost_lock()) 94 return; 95 96 if (cpu_boost_calls_count == MAX_BOOST_LOG) 97 { 98 cpu_boost_first = (cpu_boost_first+1)%MAX_BOOST_LOG; 99 cpu_boost_calls_count--; 100 if (cpu_boost_calls_count < 0) 101 cpu_boost_calls_count = 0; 102 item += cpu_boost_first; 103 } 104 if (cpu_boost_calls_count < MAX_BOOST_LOG) 105 { 106 int message = (cpu_boost_first+cpu_boost_calls_count)%MAX_BOOST_LOG; 107 snprintf(cpu_boost_calls[message], MAX_PATH,"%d.) %c %d %s:%d", 108 item,on_off?'B':'U',boost_counter,location,line); 109 cpu_boost_calls_count++; 110 } 111#else 112void cpu_boost(bool on_off) 113{ 114 if (!cpu_boost_lock()) 115 return; 116 117#endif /* CPU_BOOST_LOGGING */ 118 if(on_off) 119 { 120 /* Boost the frequency if not already boosted */ 121 if(++boost_counter == 1) 122 set_cpu_frequency(CPUFREQ_MAX); 123 } 124 else 125 { 126 /* Lower the frequency if the counter reaches 0 */ 127 if(--boost_counter <= 0) 128 { 129 if(cpu_idle) 130 set_cpu_frequency(CPUFREQ_DEFAULT); 131 else 132 set_cpu_frequency(CPUFREQ_NORMAL); 133 134 /* Safety measure */ 135 if (boost_counter < 0) 136 { 137 boost_counter = 0; 138 } 139 } 140 } 141 142 cpu_boost_unlock(); 143} 144 145void cpu_idle_mode(bool on_off) 146{ 147 if (!cpu_boost_lock()) 148 return; 149 150 cpu_idle = on_off; 151 152 /* We need to adjust the frequency immediately if the CPU 153 isn't boosted */ 154 if(boost_counter == 0) 155 { 156 if(cpu_idle) 157 set_cpu_frequency(CPUFREQ_DEFAULT); 158 else 159 set_cpu_frequency(CPUFREQ_NORMAL); 160 } 161 162 cpu_boost_unlock(); 163} 164#endif /* HAVE_ADJUSTABLE_CPU_FREQ */ 165 166 167#ifdef HAVE_FLASHED_ROCKBOX 168static bool detect_flash_header(uint8_t *addr) 169{ 170#ifndef BOOTLOADER 171 int oldmode = system_memory_guard(MEMGUARD_NONE); 172#endif 173 struct flash_header hdr; 174 memcpy(&hdr, addr, sizeof(struct flash_header)); 175#ifndef BOOTLOADER 176 system_memory_guard(oldmode); 177#endif 178 return hdr.magic == FLASH_MAGIC; 179} 180#endif 181 182bool detect_flashed_romimage(void) 183{ 184#ifdef HAVE_FLASHED_ROCKBOX 185 return detect_flash_header((uint8_t *)FLASH_ROMIMAGE_ENTRY); 186#else 187 return false; 188#endif /* HAVE_FLASHED_ROCKBOX */ 189} 190 191bool detect_flashed_ramimage(void) 192{ 193#ifdef HAVE_FLASHED_ROCKBOX 194 return detect_flash_header((uint8_t *)FLASH_RAMIMAGE_ENTRY); 195#else 196 return false; 197#endif /* HAVE_FLASHED_ROCKBOX */ 198} 199 200bool detect_original_firmware(void) 201{ 202 return !(detect_flashed_ramimage() || detect_flashed_romimage()); 203} 204