1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
/*
* File: huawei_matebook14s_codec_fix.cpp
* Author: ursul_polar
*
* Created on Jan 22, 2023, 5:34 AM
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/io.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <syslog.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/stat.h>
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
#define HDA_VERB(nid,verb,param) ((nid)<<24 | (verb)<<8 | (param))
#define HDA_IOCTL_VERB_WRITE _IOWR('H', 0x11, struct hda_verb_ioctl)
#define GET_VARIABLE_NAME(Variable) (void(Variable),#Variable)
// Struct to contain HDA_VERBS properties
struct hda_verb_ioctl {
u32 verb; /* HDA_VERB() */
u32 res; /* response */
}state_check;
// Derivate struct to handle conn and EAPD separately
struct verbs_enable{
struct hda_verb_ioctl conn;
struct hda_verb_ioctl eapd;
}speaker_enable, headphone_enable;
bool done = false;
// If TERM Signal is sent will cause the loop to exit
void trpsig(int)
{
done = true;
}
int get_conn_state(int fd){
// Set standard verb to get status for HP connection
state_check.verb = HDA_VERB(0x16, 0x0f09, 0x0);
// Check connection status
if (ioctl(fd, HDA_IOCTL_VERB_WRITE, &state_check) < 0)
syslog(LOG_ERR, "Failed to read connection state verb.");
return state_check.res >> 28;
}
int get_snd_device(){
int fd;
// Check that we can RW with regards to the sound card
fd = open("/dev/snd/hwC0D0", O_RDWR|O_NOCTTY);
if (fd < 0) {
syslog(LOG_ERR, "Failed to open snd device.");
exit(EXIT_FAILURE);
}
return fd;
}
void clear_pin(int fd, const char* type){
struct hda_verb_ioctl clear_pins;
int array[]={0x715, 0x716, 0x717};
for(int i=0; i<3; i++){
if(type == "headphone" && array[i] != 0x715)
clear_pins.verb = HDA_VERB(0x1, array[i], 0x2);
else if( type =="headphone" && array[i] == 0x715)
clear_pins.verb = HDA_VERB(0x1, array[i], 0x0);
if(type == "spkr" && array[i] == 0x715)
clear_pins.verb = HDA_VERB(0x1, array[i], 0x2);
else if( type =="spkr" && array[i] != 0x715)
continue;
if (clear_pins.verb > 0){
if (ioctl(fd, HDA_IOCTL_VERB_WRITE, &clear_pins) < 0)
syslog(LOG_ERR, "Failed to write clear pin hda verb.");
else
syslog(LOG_INFO, "Written data to hda verb %.8x", clear_pins.verb);
}
}
}
void enable_verb(int fd, struct hda_verb_ioctl conn, struct hda_verb_ioctl eapd, const char* type){
// Enable speakers with hda verbs
if (ioctl(fd, HDA_IOCTL_VERB_WRITE, &conn) < 0)
syslog(LOG_ERR, "Failed to write connection slector hda verb.");
if (ioctl(fd, HDA_IOCTL_VERB_WRITE, &eapd) < 0)
syslog(LOG_ERR, "Failed to write EAPD hda verb.");
clear_pin(fd, type);
}
int main(int argc, char** argv) {
// TERM Signal check
signal(SIGTERM, &trpsig);
// Sound card file descriptor and connection state vars
int fd, state;
// Check connection status
state = 0;
// Set standard verbs to enable connections and EAPD for speaker
speaker_enable.conn.verb = HDA_VERB(0x16, 0x701, 0x0001);
speaker_enable.eapd.verb = HDA_VERB(0x17, 0x70c, 0x0002);
// Set standard verbs to enable connections and EAPD for headphones
headphone_enable.conn.verb = HDA_VERB(0x16, 0x701, 0x0000);
headphone_enable.eapd.verb = HDA_VERB(0x17, 0x70c, 0x0000);
// Write to syslog
syslog(LOG_INFO, "Daemon started.");
// Check that we can RW with regards to the sound card
fd = get_snd_device();
// Enable Speaker on startup so that there's no need to have a audio jack inserted
// to trigger the switch
enable_verb(fd, speaker_enable.conn, speaker_enable.eapd, "spkr");
syslog(LOG_INFO, "Enabled speaker output");
// Enter loop to detect connection changes
while (!done)
{
sleep(1);
if (state != get_conn_state(fd) && state == 8){
//Enable speaker
enable_verb(fd, speaker_enable.conn, speaker_enable.eapd, "spkr");
syslog(LOG_INFO, "Enabled speaker output");
}
if(state != get_conn_state(fd) && state == 0){
//Enable headphones
enable_verb(fd, headphone_enable.conn, headphone_enable.eapd, "headphone");
syslog(LOG_INFO, "Enabled headphone output");
}
state = get_conn_state(fd);
}
// Close and exit
close(fd);
syslog(LOG_INFO, "Daemon stopped.");
exit(EXIT_SUCCESS);
}
|