Bring up nokia-sparkler SMP/cpufreq/lk2nd/cam/haptics, fix timer and EDL USB timeout

- Add 0013 early-SCM 4-core SMP + cpufreq kernel patch and clock-frequency DT nodes
- Complete nokia-sparkler lk2nd port, boot/X11 session startup, 180-deg cached camera, and 157 Hz haptic audio
- Replace root-only timer backend with permissionless sxmo_timer.sh and keypad/hook cancellation
- Fix EDL Firehose rawmode ACK, USB active-config preservation, *+# hint, and 30 s default USB timeout
- Sync a1120 (cocojamboo) prebuilts, docs, tests, and verified checksums in check-rebuild.sh
This commit is contained in:
Luxferre committed 2026-11-16 11:45:38 +02:00
1 parent ab53423e33
commit d7992904bf
56 files changed
+31850 -28281

No files matched your search

+2 -2
View File
@@ -394,8 +394,8 @@ int runCameraApp(int argc, char** argv) {
char c = 'F';
(void)::write(writePipe, &c, 1);
}
for (int s = 0; s < 5 && !vfShouldStop && gRunning && !snapRequested.load(); ++s) {
struct timespec ts {0, 20 * 1000 * 1000};
if (!vfShouldStop && gRunning && !snapRequested.load()) {
struct timespec ts {0, 10 * 1000 * 1000};
nanosleep(&ts, nullptr);
}
} else {
+376
View File
@@ -1,15 +1,24 @@
#include <alsa/asoundlib.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <glob.h>
#include <grp.h>
#include <linux/input.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
static volatile sig_atomic_t g_stop = 0;
static void handle_signal(int sig) {
@@ -133,6 +142,369 @@ static void sleep_ms_interruptible(long ms) {
}
}
static int contains_icase(const char* hay, const char* needle) {
if (!hay || !needle || !*needle) return 0;
size_t nlen = strlen(needle);
for (const char* p = hay; *p; ++p) {
size_t i = 0;
while (i < nlen && p[i] &&
tolower((unsigned char)p[i]) == tolower((unsigned char)needle[i])) {
++i;
}
if (i == nlen) return 1;
}
return 0;
}
static int is_haptic_audio_device(void) {
const char* env_ha = getenv("SXMO_VIBRATE_HAPTIC_AUDIO");
if (env_ha && *env_ha == '1') return 1;
if (env_ha && *env_ha == '0') return 0;
int fd = open("/sys/firmware/devicetree/base/compatible", O_RDONLY | O_CLOEXEC);
if (fd < 0) return 0;
char buf[256];
ssize_t n = read(fd, buf, sizeof(buf) - 1);
close(fd);
if (n <= 0) return 0;
buf[n] = '\0';
const char* p = buf;
const char* end = buf + n;
while (p < end && *p) {
if (strcmp(p, "nokia,sparkler") == 0 || strcmp(p, "nokia,leo") == 0) {
return 1;
}
p += strlen(p) + 1;
}
return 0;
}
static int is_non_speaker_audio_route(void) {
char buf[128];
const char* paths[2];
char cfg_dir[448];
char cfg_path[512];
get_sxmo_config_dir(cfg_dir, sizeof(cfg_dir));
snprintf(cfg_path, sizeof(cfg_path), "%s/audio_out_device", cfg_dir);
paths[0] = "/tmp/sxmo_audio_out_device";
paths[1] = cfg_path;
for (int i = 0; i < 2; ++i) {
FILE* fp = fopen(paths[i], "r");
if (!fp) continue;
if (fgets(buf, sizeof(buf), fp)) {
fclose(fp);
if (contains_icase(buf, "Headphone") ||
contains_icase(buf, "Headset") ||
contains_icase(buf, "Bluetooth") ||
contains_icase(buf, "bluez")) {
return 1;
}
return 0;
}
fclose(fp);
}
return 0;
}
static int is_pw_sink_muted(void) {
FILE* fp = fopen("/tmp/sxmo_pw_sink_muted", "r");
if (!fp) return 0;
int ch = fgetc(fp);
fclose(fp);
return ch == '1';
}
struct haptic_ctl_state {
snd_ctl_t* ctl;
int has_spk_sw;
long saved_spk_sw;
int changed_spk_sw;
int has_pri_mm1;
long saved_pri_mm1;
int changed_pri_mm1;
int has_rx3_mux;
unsigned int saved_rx3_mux;
int changed_rx3_mux;
int has_rx3_vol;
long saved_rx3_vol;
int changed_rx3_vol;
};
static int ctl_read_elem(snd_ctl_t* ctl, const char* name,
snd_ctl_elem_info_t* info,
snd_ctl_elem_value_t* val) {
snd_ctl_elem_info_clear(info);
snd_ctl_elem_info_set_interface(info, SND_CTL_ELEM_IFACE_MIXER);
snd_ctl_elem_info_set_name(info, name);
if (snd_ctl_elem_info(ctl, info) < 0) return -1;
snd_ctl_elem_value_clear(val);
snd_ctl_elem_value_set_interface(val, SND_CTL_ELEM_IFACE_MIXER);
snd_ctl_elem_value_set_name(val, name);
if (snd_ctl_elem_read(ctl, val) < 0) return -1;
return 0;
}
static void haptic_ensure_speaker_ctl(struct haptic_ctl_state* st) {
memset(st, 0, sizeof(*st));
if (snd_ctl_open(&st->ctl, "hw:0", 0) < 0) {
st->ctl = NULL;
return;
}
snd_ctl_elem_info_t* info = NULL;
snd_ctl_elem_value_t* val = NULL;
snd_ctl_elem_info_alloca(&info);
snd_ctl_elem_value_alloca(&val);
if (ctl_read_elem(st->ctl, "PRI_MI2S_RX Audio Mixer MultiMedia1",
info, val) == 0) {
st->has_pri_mm1 = 1;
st->saved_pri_mm1 = snd_ctl_elem_value_get_boolean(val, 0);
if (st->saved_pri_mm1 == 0) {
snd_ctl_elem_value_set_boolean(val, 0, 1);
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_pri_mm1 = 1;
}
}
if (ctl_read_elem(st->ctl, "RX3 MIX1 INP1", info, val) == 0) {
st->has_rx3_mux = 1;
st->saved_rx3_mux = snd_ctl_elem_value_get_enumerated(val, 0);
if (st->saved_rx3_mux == 0) {
unsigned int items = snd_ctl_elem_info_get_items(info);
for (unsigned int i = 0; i < items; ++i) {
snd_ctl_elem_info_set_item(info, i);
if (snd_ctl_elem_info(st->ctl, info) == 0 &&
strcmp(snd_ctl_elem_info_get_item_name(info), "RX1") == 0) {
snd_ctl_elem_value_set_enumerated(val, 0, i);
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_rx3_mux = 1;
break;
}
}
}
}
if (ctl_read_elem(st->ctl, "SPK DAC Switch", info, val) == 0) {
st->has_spk_sw = 1;
st->saved_spk_sw = snd_ctl_elem_value_get_boolean(val, 0);
if (st->saved_spk_sw == 0) {
snd_ctl_elem_value_set_boolean(val, 0, 1);
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_spk_sw = 1;
}
}
if (ctl_read_elem(st->ctl, "RX3 Digital Volume", info, val) == 0) {
st->has_rx3_vol = 1;
st->saved_rx3_vol = snd_ctl_elem_value_get_integer(val, 0);
if (st->saved_rx3_vol < 80) {
long maxv = snd_ctl_elem_info_get_max(info);
long target = (maxv >= 88) ? 88 : maxv;
snd_ctl_elem_value_set_integer(val, 0, target);
if (snd_ctl_elem_write(st->ctl, val) >= 0) st->changed_rx3_vol = 1;
}
}
}
static void haptic_restore_speaker_ctl(struct haptic_ctl_state* st) {
if (!st || !st->ctl) return;
snd_ctl_elem_info_t* info = NULL;
snd_ctl_elem_value_t* val = NULL;
snd_ctl_elem_info_alloca(&info);
snd_ctl_elem_value_alloca(&val);
if (st->changed_rx3_vol && st->has_rx3_vol &&
ctl_read_elem(st->ctl, "RX3 Digital Volume", info, val) == 0) {
snd_ctl_elem_value_set_integer(val, 0, st->saved_rx3_vol);
(void)snd_ctl_elem_write(st->ctl, val);
}
if (st->changed_spk_sw && st->has_spk_sw &&
ctl_read_elem(st->ctl, "SPK DAC Switch", info, val) == 0) {
snd_ctl_elem_value_set_boolean(val, 0, st->saved_spk_sw);
(void)snd_ctl_elem_write(st->ctl, val);
}
if (st->changed_rx3_mux && st->has_rx3_mux &&
ctl_read_elem(st->ctl, "RX3 MIX1 INP1", info, val) == 0) {
snd_ctl_elem_value_set_enumerated(val, 0, st->saved_rx3_mux);
(void)snd_ctl_elem_write(st->ctl, val);
}
if (st->changed_pri_mm1 && st->has_pri_mm1 &&
ctl_read_elem(st->ctl, "PRI_MI2S_RX Audio Mixer MultiMedia1",
info, val) == 0) {
snd_ctl_elem_value_set_boolean(val, 0, st->saved_pri_mm1);
(void)snd_ctl_elem_write(st->ctl, val);
}
snd_ctl_close(st->ctl);
st->ctl = NULL;
}
static int write_pcm_frames_all(snd_pcm_t* pcm, const short* buf,
snd_pcm_uframes_t frames) {
snd_pcm_uframes_t off = 0;
while (off < frames && !g_stop) {
snd_pcm_sframes_t rc = snd_pcm_writei(pcm, buf + off * 2, frames - off);
if (rc == -EAGAIN || rc == -EINTR) {
continue;
}
if (rc < 0) {
rc = snd_pcm_recover(pcm, (int)rc, 1);
if (rc < 0) return -1;
continue;
}
off += (snd_pcm_uframes_t)rc;
}
return g_stop ? -1 : 0;
}
static int write_haptic_pulse(snd_pcm_t* pcm, long ms, double amp) {
if (ms <= 0 || g_stop) return 0;
const long rate = 48000L;
const double step = 2.0 * M_PI * 157.0 / (double)rate;
const long total_frames = (ms * rate) / 1000L;
long fade_frames = (5L * rate) / 1000L;
if (fade_frames > total_frames / 2) fade_frames = total_frames / 2;
short chunk[2400 * 2];
long pos = 0;
while (pos < total_frames && !g_stop) {
long n = total_frames - pos;
if (n > 2400L) n = 2400L;
for (long i = 0; i < n; ++i) {
long idx = pos + i;
double env = 1.0;
if (fade_frames > 0 && idx < fade_frames) {
env = 0.5 * (1.0 - cos(M_PI * (double)idx / (double)fade_frames));
} else if (fade_frames > 0 && idx >= total_frames - fade_frames) {
long tail = total_frames - 1 - idx;
env = 0.5 * (1.0 - cos(M_PI * (double)tail / (double)fade_frames));
}
double v = env * amp * sin(step * (double)idx);
if (v > 32767.0) v = 32767.0;
if (v < -32767.0) v = -32767.0;
short s = (short)lrint(v);
chunk[i * 2] = s;
chunk[i * 2 + 1] = s;
}
if (write_pcm_frames_all(pcm, chunk, (snd_pcm_uframes_t)n) < 0) {
return -1;
}
pos += n;
}
return 0;
}
static int write_haptic_silence(snd_pcm_t* pcm, long ms) {
if (ms <= 0 || g_stop) return 0;
const long total_frames = (ms * 48000L) / 1000L;
short zeros[2400 * 2];
memset(zeros, 0, sizeof(zeros));
long pos = 0;
while (pos < total_frames && !g_stop) {
long n = total_frames - pos;
if (n > 2400L) n = 2400L;
if (write_pcm_frames_all(pcm, zeros, (snd_pcm_uframes_t)n) < 0) {
return -1;
}
pos += n;
}
return 0;
}
static int vibrate_haptic_audio(long duration_ms, int level) {
if (is_non_speaker_audio_route()) {
return 0;
}
/* Match PipeWire socket ownership when invoked from a root hook/shell. */
if (getuid() == 0 && access("/run/user/10000/pipewire-0", F_OK) == 0) {
(void)initgroups("user", 10000);
if (setgid(10000) == 0 && setuid(10000) == 0) {
(void)setenv("XDG_RUNTIME_DIR", "/run/user/10000", 1);
}
} else {
const char* xdg = getenv("XDG_RUNTIME_DIR");
struct stat st_rt;
if (!xdg || !*xdg || stat(xdg, &st_rt) != 0) {
char rt_buf[64];
snprintf(rt_buf, sizeof(rt_buf), "/run/user/%lu",
(unsigned long)getuid());
(void)setenv("XDG_RUNTIME_DIR", rt_buf, 1);
}
}
struct haptic_ctl_state ctl_st;
memset(&ctl_st, 0, sizeof(ctl_st));
snd_pcm_t* pcm = NULL;
const char* devs[2];
if (is_pw_sink_muted()) {
devs[0] = "plughw:0,0";
devs[1] = "default";
} else {
devs[0] = "default";
devs[1] = "plughw:0,0";
}
const char* opened_dev = NULL;
for (int i = 0; i < 2 && !pcm; ++i) {
if (snd_pcm_open(&pcm, devs[i], SND_PCM_STREAM_PLAYBACK, 0) == 0) {
if (snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE,
SND_PCM_ACCESS_RW_INTERLEAVED, 2, 48000,
1, 500000) < 0) {
snd_pcm_close(pcm);
pcm = NULL;
} else {
opened_dev = devs[i];
}
}
}
if (!pcm) {
fprintf(stderr, "sxmo_vibrate: cannot open haptic audio PCM\n");
return 1;
}
if (opened_dev && strcmp(opened_dev, "plughw:0,0") == 0) {
haptic_ensure_speaker_ctl(&ctl_st);
}
double amp = 21000.0;
if (level <= 1) amp = 13000.0;
else if (level >= 3) amp = 29000.0;
if (level >= 3 || duration_ms <= 90) {
long actual_ms = (level == 1 && duration_ms > 55) ? 55 : duration_ms;
(void)write_haptic_pulse(pcm, actual_ms, amp);
} else {
long on_ms = (level == 1) ? 75 : 160;
long off_ms = (level == 1) ? 125 : 80;
long rem = duration_ms;
while (rem > 0 && !g_stop) {
long cur_on = (rem < on_ms) ? rem : on_ms;
if (write_haptic_pulse(pcm, cur_on, amp) < 0) break;
rem -= cur_on;
if (rem <= 0 || g_stop) break;
long cur_off = (rem < off_ms) ? rem : off_ms;
if (write_haptic_silence(pcm, cur_off) < 0) break;
rem -= cur_off;
}
}
if (g_stop) {
(void)snd_pcm_drop(pcm);
} else {
(void)snd_pcm_drain(pcm);
}
snd_pcm_close(pcm);
haptic_restore_speaker_ctl(&ctl_st);
return 0;
}
int main(int argc, char** argv) {
if (argc < 2 || argc > 3) {
fprintf(stderr, "Usage: sxmo_vibrate duration_ms [strength_number]\n");
@@ -174,6 +546,10 @@ int main(int argc, char** argv) {
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
if (is_haptic_audio_device()) {
return vibrate_haptic_audio(duration_ms, level);
}
int fd = open_vibrator_device();
if (fd < 0) {
fprintf(stderr, "sxmo_vibrate: cannot open vibrator device: %s\n",