Skip to content

Commit 310004b

Browse files
committed
external: control the board using external program
Control the board using external program. It will be called with 3 arguments: <board> <command> <on/off>, where command is one of "power", "usb", "key-fastboot", "key-power". Signed-off-by: Dmitry Baryshkov <[email protected]>
1 parent 8663f5c commit 310004b

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed

device.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ extern const struct control_ops alpaca_ops;
9090
extern const struct control_ops cdb_assist_ops;
9191
extern const struct control_ops conmux_ops;
9292
extern const struct control_ops ftdi_gpio_ops;
93+
extern const struct control_ops external_ops;
9394
extern const struct control_ops qcomlt_dbg_ops;
9495

9596
extern const struct console_ops conmux_console_ops;

device_parser.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ static void parse_board(struct device_parser *dp)
147147
} else if (!strcmp(key, "ftdi_gpio")) {
148148
dev->control_dev = strdup(value);
149149
set_control_ops(dev, &ftdi_gpio_ops);
150+
} else if (!strcmp(key, "external")) {
151+
dev->control_dev = strdup(value);
152+
set_control_ops(dev, &external_ops);
150153
} else if (!strcmp(key, "qcomlt_debug_board")) {
151154
dev->control_dev = strdup(value);
152155
set_control_ops(dev, &qcomlt_dbg_ops);

external.c

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright (c) 2021-2023, Linaro Ltd.
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
*
8+
* 1. Redistributions of source code must retain the above copyright notice,
9+
* this list of conditions and the following disclaimer.
10+
*
11+
* 2. Redistributions in binary form must reproduce the above copyright notice,
12+
* this list of conditions and the following disclaimer in the documentation
13+
* and/or other materials provided with the distribution.
14+
*
15+
* 3. Neither the name of the copyright holder nor the names of its contributors
16+
* may be used to endorse or promote products derived from this software without
17+
* specific prior written permission.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#include <sys/types.h>
33+
#include <sys/wait.h>
34+
#include <unistd.h>
35+
36+
#include <errno.h>
37+
#include <stdlib.h>
38+
39+
#include "cdba-server.h"
40+
#include "device.h"
41+
42+
struct external {
43+
const char *path;
44+
const char *board;
45+
};
46+
47+
static int external_helper(struct external *ext, const char *command, bool on)
48+
{
49+
pid_t pid, pid_ret;
50+
int status;
51+
52+
pid = fork();
53+
switch (pid) {
54+
case 0:
55+
/* Do not clobber stdout with program messages or cdba will become confused */
56+
dup2(2, 1);
57+
return execlp(ext->path, ext->path, ext->board, command, on ? "on": "off", NULL);
58+
case -1:
59+
return -1;
60+
default:
61+
break;
62+
}
63+
64+
pid_ret = waitpid(pid, &status, 0);
65+
if (pid_ret < 0)
66+
return pid_ret;
67+
68+
if (WIFEXITED(status))
69+
return WEXITSTATUS(status);
70+
else if (WIFSIGNALED(status))
71+
errno = -EINTR;
72+
else
73+
errno = -EIO;
74+
75+
return -1;
76+
}
77+
78+
static void *external_open(struct device *dev)
79+
{
80+
struct external *ext;
81+
82+
dev->has_power_key = true;
83+
84+
ext = calloc(1, sizeof(*ext));
85+
86+
ext->path = dev->control_dev;
87+
ext->board = dev->board;
88+
89+
return ext;
90+
}
91+
92+
static int external_power(struct device *dev, bool on)
93+
{
94+
struct external *ext = dev->cdb;
95+
96+
return external_helper(ext, "power", on);
97+
}
98+
99+
static void external_usb(struct device *dev, bool on)
100+
{
101+
struct external *ext = dev->cdb;
102+
103+
external_helper(ext, "usb", on);
104+
}
105+
106+
static void external_key(struct device *dev, int key, bool asserted)
107+
{
108+
struct external *ext = dev->cdb;
109+
110+
switch (key) {
111+
case DEVICE_KEY_FASTBOOT:
112+
external_helper(ext, "key-fastboot", asserted);
113+
break;
114+
case DEVICE_KEY_POWER:
115+
external_helper(ext, "key-power", asserted);
116+
break;
117+
}
118+
}
119+
120+
const struct control_ops external_ops = {
121+
.open = external_open,
122+
.power = external_power,
123+
.usb = external_usb,
124+
.key = external_key,
125+
};

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ server_srcs = ['cdba-server.c',
6161
'conmux.c',
6262
'device.c',
6363
'device_parser.c',
64+
'external.c',
6465
'fastboot.c',
6566
'alpaca.c',
6667
'ftdi-gpio.c',

0 commit comments

Comments
 (0)