diff --git a/cdba-power.c b/cdba-power.c new file mode 100644 index 0000000..cd907bf --- /dev/null +++ b/cdba-power.c @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2024, Linaro Ltd. + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include + +#include "cdba-server.h" +#include "device.h" +#include "device_parser.h" +#include "watch.h" + +void cdba_send_buf(int type, size_t len, const void *buf) +{ + /* ignore console messages */ +} + +static void usage(const char *name) +{ + fprintf(stderr, "Usage: %s on|off\n", name); + exit(EXIT_FAILURE); +} + +static struct device *selected_device; + +bool ready(void) +{ + return device_is_running(selected_device); +} + +int main(int argc, char **argv) +{ + const char *home; + const char *name; + bool on; + int ret; + + if (argc != 3) + usage(argv[0]); + + if (!strcmp(argv[2], "on")) + on = true; + else if (!strcmp(argv[2], "off")) + on = false; + else + usage(argv[0]); + + home = getenv("HOME"); + if (home) + chdir(home); + + ret = device_parser(".cdba"); + if (ret) { + ret = device_parser("/etc/cdba"); + if (ret) { + fprintf(stderr, "device parser: unable to open config file\n"); + exit(1); + } + } + + name = argv[1]; + selected_device = device_open(name, "nobody"); + if (!selected_device) { + fprintf(stderr, "failed to open %s\n", name); + exit(EXIT_FAILURE); + } + + if (on) { + device_power(selected_device, true); + watch_main_loop(ready); + + selected_device->usb_always_on = true; + selected_device->power_always_on = true; + } else { + device_usb(selected_device, false); + device_power(selected_device, false); + } + + device_close(selected_device); + + return 0; +} diff --git a/cdba-server.c b/cdba-server.c index c737e5b..2b16661 100644 --- a/cdba-server.c +++ b/cdba-server.c @@ -2,34 +2,8 @@ * Copyright (c) 2016-2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ -#include -#include #include #include #include @@ -46,40 +20,12 @@ #include "device_parser.h" #include "fastboot.h" #include "list.h" +#include "watch.h" -static bool quit_invoked; static const char *username; struct device *selected_device; -int tty_open(const char *tty, struct termios *old) -{ - struct termios tios; - int ret; - int fd; - - fd = open(tty, O_RDWR | O_NOCTTY | O_EXCL); - if (fd < 0) - err(1, "unable to open \"%s\"", tty); - - ret = tcgetattr(fd, old); - if (ret < 0) - err(1, "unable to retrieve \"%s\" tios", tty); - - memset(&tios, 0, sizeof(tios)); - tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD; - tios.c_iflag = IGNPAR; - tios.c_oflag = 0; - - tcflush(fd, TCIFLUSH); - - ret = tcsetattr(fd, TCSANOW, &tios); - if (ret < 0) - err(1, "unable to update \"%s\" tios", tty); - - return fd; -} - static void fastboot_opened(struct fastboot *fb, void *data) { const uint8_t one = 1; @@ -109,12 +55,14 @@ static struct fastboot_ops fastboot_ops = { static void msg_select_board(const void *param) { - selected_device = device_open(param, username, &fastboot_ops); + selected_device = device_open(param, username); if (!selected_device) { fprintf(stderr, "failed to open %s\n", (const char *)param); - quit_invoked = true; + watch_quit(); } + device_fastboot_open(selected_device, &fastboot_ops); + cdba_send(MSG_SELECT_BOARD); } @@ -248,119 +196,14 @@ static int handle_stdin(int fd, void *buf) return 0; } -struct watch { - struct list_head node; - - int fd; - int (*cb)(int, void*); - void *data; -}; - -struct timer { - struct list_head node; - struct timeval tv; - - void (*cb)(void *); - void *data; -}; - -static struct list_head read_watches = LIST_INIT(read_watches); -static struct list_head timer_watches = LIST_INIT(timer_watches); - -void watch_add_readfd(int fd, int (*cb)(int, void*), void *data) -{ - struct watch *w; - - w = calloc(1, sizeof(*w)); - w->fd = fd; - w->cb = cb; - w->data = data; - - list_add(&read_watches, &w->node); -} - -void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data) -{ - struct timeval tv_timeout; - struct timeval now; - struct timer *t; - - t = calloc(1, sizeof(*t)); - - gettimeofday(&now, NULL); - - tv_timeout.tv_sec = timeout_ms / 1000; - tv_timeout.tv_usec = (timeout_ms % 1000) * 1000; - - t->cb = cb; - t->data = data; - timeradd(&now, &tv_timeout, &t->tv); - - list_add(&timer_watches, &t->node); -} - -static struct timeval *watch_timer_next(void) -{ - static struct timeval timeout; - struct timeval now; - struct timer *next; - struct timer *t; - - if (list_empty(&timer_watches)) - return NULL; - - next = list_entry_first(&timer_watches, struct timer, node); - - list_for_each_entry(t, &timer_watches, node) { - if (timercmp(&t->tv, &next->tv, <)) - next = t; - } - - gettimeofday(&now, NULL); - timersub(&next->tv, &now, &timeout); - if (timeout.tv_sec < 0) { - timeout.tv_sec = 0; - timeout.tv_usec = 0; - } - - return &timeout; -} - -static void watch_timer_invoke(void) -{ - struct timeval now; - struct timer *tmp; - struct timer *t; - - gettimeofday(&now, NULL); - - list_for_each_entry_safe(t, tmp, &timer_watches, node) { - if (timercmp(&t->tv, &now, <)) { - t->cb(t->data); - - list_del(&t->node); - free(t); - } - } -} - static void sigpipe_handler(int signo) { - quit_invoked = true; -} - -void watch_quit(void) -{ - quit_invoked = true; + watch_quit(); } int main(int argc, char **argv) { - struct timeval *timeoutp; - struct watch *w; - fd_set rfds; int flags; - int nfds; int ret; signal(SIGPIPE, sigpipe_handler); @@ -389,40 +232,7 @@ int main(int argc, char **argv) flags = fcntl(STDIN_FILENO, F_GETFL, 0); fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK); - while (!quit_invoked) { - nfds = 0; - - list_for_each_entry(w, &read_watches, node) { - nfds = MAX(nfds, w->fd); - FD_SET(w->fd, &rfds); - } - - if (!FD_ISSET(STDIN_FILENO, &rfds)) { - fprintf(stderr, "rfds is trash!\n"); - goto done; - } - - timeoutp = watch_timer_next(); - ret = select(nfds + 1, &rfds, NULL, NULL, timeoutp); - if (ret < 0 && errno == EINTR) - continue; - else if (ret < 0) - break; - - watch_timer_invoke(); - - list_for_each_entry(w, &read_watches, node) { - if (FD_ISSET(w->fd, &rfds)) { - ret = w->cb(w->fd, w->data); - if (ret < 0) { - fprintf(stderr, "cb returned %d\n", ret); - goto done; - } - } - } - } - -done: + watch_run(); /* if we got here, stdin/out/err might be not accessible anymore */ ret = open("/dev/null", O_RDWR); diff --git a/cdba-server.h b/cdba-server.h index 0dd6f26..4176311 100644 --- a/cdba-server.h +++ b/cdba-server.h @@ -6,14 +6,6 @@ #include "cdba.h" -void watch_add_readfd(int fd, int (*cb)(int, void*), void *data); -int watch_add_quit(int (*cb)(int, void*), void *data); -void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data); -void watch_quit(void); -int watch_run(void); - -int tty_open(const char *tty, struct termios *old); - void cdba_send_buf(int type, size_t len, const void *buf); #define cdba_send(type) cdba_send_buf(type, 0, NULL) diff --git a/cdba.c b/cdba.c index 681ed78..1809423 100644 --- a/cdba.c +++ b/cdba.c @@ -2,31 +2,7 @@ * Copyright (c) 2016-2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include diff --git a/circ_buf.c b/circ_buf.c index 08766be..5cda4cf 100644 --- a/circ_buf.c +++ b/circ_buf.c @@ -2,31 +2,7 @@ * Copyright (c) 2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include diff --git a/circ_buf.h b/circ_buf.h index a169d04..f4a3b20 100644 --- a/circ_buf.h +++ b/circ_buf.h @@ -2,31 +2,7 @@ * Copyright (c) 2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #ifndef __CIRC_BUF_H__ #define __CIRC_BUF_H__ diff --git a/console.c b/console.c index d758c8b..3ff0a2f 100644 --- a/console.c +++ b/console.c @@ -2,31 +2,7 @@ * Copyright (c) 2020, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include @@ -37,6 +13,8 @@ #include "cdba-server.h" #include "device.h" +#include "tty.h" +#include "watch.h" struct console { int console_fd; diff --git a/device.c b/device.c index 9f4bcab..848f552 100644 --- a/device.c +++ b/device.c @@ -2,31 +2,7 @@ * Copyright (c) 2016-2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include @@ -47,6 +23,7 @@ #include "list.h" #include "ppps.h" #include "status-cmd.h" +#include "watch.h" #define ARRAY_SIZE(x) ((sizeof(x)/sizeof((x)[0]))) @@ -117,9 +94,10 @@ static bool device_check_access(struct device *device, return false; } +static int device_power_off(struct device *device); + struct device *device_open(const char *board, - const char *username, - struct fastboot_ops *fastboot_ops) + const char *username) { struct device *device; @@ -156,11 +134,21 @@ found: if (!device->console) errx(1, "failed to open device console"); + /* + * Power off before opening fastboot. Otherwise if the device is + * already in the fastboot state, CDBA will detect it, then power up + * procedure will restart the device causing fastboot to disappear and + * appear again. This will cause CDBA to exit, ending up with the + * unbreakable fastboot-reset-second_fastboot-quit cycle. + * */ + if (device->power_always_on) { + device_power_off(device); + sleep(2); + } + if (device->usb_always_on) device_usb(device, true); - device->fastboot = fastboot_open(device->serial, fastboot_ops, NULL); - return device; } @@ -239,6 +227,11 @@ static void device_tick(void *data) } } +bool device_is_running(struct device *device) +{ + return device->state == DEVICE_STATE_RUNNING; +} + static int device_power_on(struct device *device) { if (!device || !device_has_control(device, power)) @@ -298,18 +291,36 @@ int device_write(struct device *device, const void *buf, size_t len) return device_console(device, write, buf, len); } +void device_fastboot_open(struct device *device, + struct fastboot_ops *fastboot_ops) +{ + device->fastboot = fastboot_open(device->serial, fastboot_ops, NULL); +} + void device_fastboot_boot(struct device *device) { + if (!device->fastboot) { + fprintf(stderr, "fastboot not opened\n"); + return; + } fastboot_boot(device->fastboot); } void device_fastboot_continue(struct device *device) { + if (!device->fastboot) { + fprintf(stderr, "fastboot not opened\n"); + return; + } fastboot_continue(device->fastboot); } void device_fastboot_flash_reboot(struct device *device) { + if (!device->fastboot) { + fprintf(stderr, "fastboot not opened\n"); + return; + } fastboot_flash(device->fastboot, "boot"); fastboot_reboot(device->fastboot); } @@ -382,7 +393,8 @@ void device_close(struct device *dev) { if (!dev->usb_always_on) device_usb(dev, false); - device_power(dev, false); + if (!dev->power_always_on) + device_power(dev, false); if (device_has_control(dev, close)) device_control(dev, close); diff --git a/device.h b/device.h index 6d431bc..0edb048 100644 --- a/device.h +++ b/device.h @@ -41,6 +41,7 @@ struct device { unsigned voltage; bool tickle_mmc; bool usb_always_on; + bool power_always_on; struct fastboot *fastboot; unsigned int fastboot_key_timeout; int state; @@ -72,8 +73,7 @@ struct device_user { void device_add(struct device *device); struct device *device_open(const char *board, - const char *username, - struct fastboot_ops *fastboot_ops); + const char *username); void device_close(struct device *dev); int device_power(struct device *device, bool on); @@ -83,12 +83,15 @@ int device_write(struct device *device, const void *buf, size_t len); void device_boot(struct device *device, const void *data, size_t len); +void device_fastboot_open(struct device *device, + struct fastboot_ops *fastboot_ops); void device_fastboot_boot(struct device *device); void device_fastboot_flash_reboot(struct device *device); void device_send_break(struct device *device); void device_list_devices(const char *username); void device_info(const char *username, const void *data, size_t dlen); void device_fastboot_continue(struct device *device); +bool device_is_running(struct device *device); enum { DEVICE_KEY_FASTBOOT, diff --git a/device_parser.c b/device_parser.c index fd65816..8b55741 100644 --- a/device_parser.c +++ b/device_parser.c @@ -2,31 +2,7 @@ * Copyright (c) 2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include @@ -195,6 +171,8 @@ static void parse_board(struct device_parser *dp) dev->ppps3_path = strdup(value); } else if (!strcmp(key, "status-cmd")) { dev->status_cmd = strdup(value); + } else if (!strcmp(key, "power_always_on")) { + dev->power_always_on = !strcmp(value, "true"); } else { fprintf(stderr, "device parser: unknown key \"%s\"\n", key); exit(1); diff --git a/alpaca.c b/drivers/alpaca.c similarity index 61% rename from alpaca.c rename to drivers/alpaca.c index 12f4c99..f720670 100644 --- a/alpaca.c +++ b/drivers/alpaca.c @@ -2,31 +2,7 @@ * Copyright (c) 2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include @@ -41,8 +17,8 @@ #include #include -#include "cdba-server.h" #include "device.h" +#include "tty.h" struct alpaca { int alpaca_fd; diff --git a/cdb_assist.c b/drivers/cdb_assist.c similarity index 82% rename from cdb_assist.c rename to drivers/cdb_assist.c index 40ed67c..2a4759d 100644 --- a/cdb_assist.c +++ b/drivers/cdb_assist.c @@ -2,31 +2,7 @@ * Copyright (c) 2016-2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include @@ -41,9 +17,10 @@ #include #include -#include "cdba-server.h" #include "device.h" #include "status.h" +#include "tty.h" +#include "watch.h" struct cdb_assist { char serial[9]; diff --git a/conmux.c b/drivers/conmux.c similarity index 81% rename from conmux.c rename to drivers/conmux.c index 1cd8504..552f245 100644 --- a/conmux.c +++ b/drivers/conmux.c @@ -2,31 +2,7 @@ * Copyright (c) 2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include @@ -42,6 +18,7 @@ #include "cdba-server.h" #include "device.h" +#include "watch.h" extern int h_errno; diff --git a/external.c b/drivers/external.c similarity index 54% rename from external.c rename to drivers/external.c index 0fb82e1..793e348 100644 --- a/external.c +++ b/drivers/external.c @@ -2,31 +2,7 @@ * Copyright (c) 2021-2023, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include diff --git a/ftdi-gpio.c b/drivers/ftdi-gpio.c similarity index 87% rename from ftdi-gpio.c rename to drivers/ftdi-gpio.c index 050e2db..77e5384 100644 --- a/ftdi-gpio.c +++ b/drivers/ftdi-gpio.c @@ -2,31 +2,7 @@ * Copyright (c) 2023, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #define _GNU_SOURCE #include diff --git a/local-gpio-v1.c b/drivers/local-gpio-v1.c similarity index 51% rename from local-gpio-v1.c rename to drivers/local-gpio-v1.c index b706f65..793dd1d 100644 --- a/local-gpio-v1.c +++ b/drivers/local-gpio-v1.c @@ -2,31 +2,7 @@ * Copyright (c) 2023, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include diff --git a/local-gpio-v2.c b/drivers/local-gpio-v2.c similarity index 63% rename from local-gpio-v2.c rename to drivers/local-gpio-v2.c index 5e11c60..64cae88 100644 --- a/local-gpio-v2.c +++ b/drivers/local-gpio-v2.c @@ -2,31 +2,7 @@ * Copyright (c) 2023, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #define _GNU_SOURCE #include diff --git a/local-gpio.c b/drivers/local-gpio.c similarity index 72% rename from local-gpio.c rename to drivers/local-gpio.c index ef1c50c..a9ddcc4 100644 --- a/local-gpio.c +++ b/drivers/local-gpio.c @@ -2,31 +2,7 @@ * Copyright (c) 2023, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include diff --git a/drivers/local-gpio.h b/drivers/local-gpio.h new file mode 100644 index 0000000..879e51d --- /dev/null +++ b/drivers/local-gpio.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023, Linaro Ltd. + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#ifndef _LOCAL_GPIO_H_ +#define _LOCAL_GPIO_H_ + +enum { + GPIO_POWER = 0, // Power input enable + GPIO_FASTBOOT_KEY, // Usually volume key + GPIO_POWER_KEY, // Key to power the device + GPIO_USB_DISCONNECT, // Simulate main USB connection + GPIO_COUNT +}; + +struct local_gpio_options { + struct { + char *chip; + bool present; + unsigned int offset; + bool active_low; + } gpios[GPIO_COUNT]; +}; + +struct local_gpio { + struct local_gpio_options *options; + struct { + void *chip; + void *line; + } gpios[GPIO_COUNT]; +}; + +int local_gpio_init(struct local_gpio *local_gpio); +int local_gpio_set_value(struct local_gpio *local_gpio, unsigned int gpio, bool on); + +#endif /* _LOCAL_GPIO_H_ */ diff --git a/qcomlt_dbg.c b/drivers/qcomlt_dbg.c similarity index 74% rename from qcomlt_dbg.c rename to drivers/qcomlt_dbg.c index 302e77f..fe55cf4 100644 --- a/qcomlt_dbg.c +++ b/drivers/qcomlt_dbg.c @@ -2,31 +2,7 @@ * Copyright (c) 2021, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include @@ -45,6 +21,8 @@ #include "cdba-server.h" #include "device.h" #include "status.h" +#include "tty.h" +#include "watch.h" enum qcomlt_parse_state { STATE_, diff --git a/fastboot.c b/fastboot.c index eec8947..32f08f8 100644 --- a/fastboot.c +++ b/fastboot.c @@ -2,31 +2,7 @@ * Copyright (c) 2016-2018, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include #include @@ -46,6 +22,7 @@ #include "cdba-server.h" #include "fastboot.h" +#include "watch.h" #define MAX_USBFS_BULK_SIZE (16*1024) diff --git a/list.h b/list.h index 14b9095..c6930c4 100644 --- a/list.h +++ b/list.h @@ -2,31 +2,7 @@ * Copyright (c) 2016, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #ifndef __LIST_H__ #define __LIST_H__ diff --git a/local-gpio.h b/local-gpio.h deleted file mode 100644 index 44d9cc6..0000000 --- a/local-gpio.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * Copyright (c) 2023, Linaro Ltd. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef _LOCAL_GPIO_H_ -#define _LOCAL_GPIO_H_ - -enum { - GPIO_POWER = 0, // Power input enable - GPIO_FASTBOOT_KEY, // Usually volume key - GPIO_POWER_KEY, // Key to power the device - GPIO_USB_DISCONNECT, // Simulate main USB connection - GPIO_COUNT -}; - -struct local_gpio_options { - struct { - char *chip; - bool present; - unsigned int offset; - bool active_low; - } gpios[GPIO_COUNT]; -}; - -struct local_gpio { - struct local_gpio_options *options; - struct { - void *chip; - void *line; - } gpios[GPIO_COUNT]; -}; - -int local_gpio_init(struct local_gpio *local_gpio); -int local_gpio_set_value(struct local_gpio *local_gpio, unsigned int gpio, bool on); - -#endif /* _LOCAL_GPIO_H_ */ diff --git a/meson.build b/meson.build index f1d12bd..fe03e81 100644 --- a/meson.build +++ b/meson.build @@ -54,7 +54,7 @@ if not ftdi_dep.found() endif gpiod_dep = dependency('libgpiod', required: server_opt) -server_deps = [dependency('libudev', required: server_opt), +cdbalib_deps = [dependency('libudev', required: server_opt), dependency('yaml-0.1', required: server_opt), gpiod_dep, ftdi_dep] @@ -62,43 +62,57 @@ server_deps = [dependency('libudev', required: server_opt), # E.g. Debian reuires -lutil for forkpty if not compiler.has_function('forkpty') util_dep = compiler.find_library('util') - server_deps += util_dep + cdbalib_deps += util_dep endif -server_srcs = ['cdba-server.c', - 'cdb_assist.c', - 'circ_buf.c', - 'conmux.c', - 'device.c', - 'device_parser.c', - 'external.c', - 'fastboot.c', - 'alpaca.c', - 'ftdi-gpio.c', - 'local-gpio.c', - 'console.c', - 'qcomlt_dbg.c', - 'ppps.c', - 'status.c', - 'status-cmd.c'] +drivers_srcs = ['drivers/alpaca.c', + 'drivers/cdb_assist.c', + 'drivers/conmux.c', + 'drivers/external.c', + 'drivers/ftdi-gpio.c', + 'drivers/local-gpio.c', + 'drivers/qcomlt_dbg.c', + ] if gpiod_dep.version().version_compare('>=2.0') - server_srcs += ['local-gpio-v2.c'] + drivers_srcs += ['drivers/local-gpio-v2.c'] else - server_srcs += ['local-gpio-v1.c'] + drivers_srcs += ['drivers/local-gpio-v1.c'] endif +cdbalib_srcs = ['circ_buf.c', + 'device.c', + 'device_parser.c', + 'fastboot.c', + 'console.c', + 'ppps.c', + 'status.c', + 'status-cmd.c', + 'watch.c', + 'tty.c'] + +server_srcs = ['cdba-server.c'] + build_server = true -foreach d: server_deps +foreach d: cdbalib_deps if not d.found() build_server = false endif endforeach if build_server + libcdba = static_library('cdba', + cdbalib_srcs + drivers_srcs, + dependencies : cdbalib_deps, + ) + executable('cdba-server', server_srcs, - dependencies : server_deps, + link_with : libcdba, + install : true) + executable('cdba-power', + ['cdba-power.c'], + link_with : libcdba, install : true) elif not server_opt.disabled() message('Skipping CDBA server build') diff --git a/ppps.c b/ppps.c index 7d8856a..b1244fc 100644 --- a/ppps.c +++ b/ppps.c @@ -2,31 +2,7 @@ * Copyright (c) 2023, Linaro Ltd. * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #define _GNU_SOURCE /* for asprintf */ diff --git a/status-cmd.c b/status-cmd.c index 3529a63..09998b8 100644 --- a/status-cmd.c +++ b/status-cmd.c @@ -2,31 +2,7 @@ * Copyright (c) 2023, Qualcomm Innovaction Center, Inc * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * - * 3. Neither the name of the copyright holder nor the names of its contributors - * may be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ #include @@ -41,6 +17,7 @@ #include "device.h" #include "status.h" #include "status-cmd.h" +#include "watch.h" static void launch_status_cmd(struct device *dev) { diff --git a/tty.c b/tty.c new file mode 100644 index 0000000..4c75cfe --- /dev/null +++ b/tty.c @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2016-2018, Linaro Ltd. + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include + +#include "tty.h" + +int tty_open(const char *tty, struct termios *old) +{ + struct termios tios; + int ret; + int fd; + + fd = open(tty, O_RDWR | O_NOCTTY | O_EXCL); + if (fd < 0) + err(1, "unable to open \"%s\"", tty); + + ret = tcgetattr(fd, old); + if (ret < 0) + err(1, "unable to retrieve \"%s\" tios", tty); + + memset(&tios, 0, sizeof(tios)); + tios.c_cflag = B115200 | CS8 | CLOCAL | CREAD; + tios.c_iflag = IGNPAR; + tios.c_oflag = 0; + + tcflush(fd, TCIFLUSH); + + ret = tcsetattr(fd, TCSANOW, &tios); + if (ret < 0) + err(1, "unable to update \"%s\" tios", tty); + + return fd; +} diff --git a/tty.h b/tty.h new file mode 100644 index 0000000..6345fc0 --- /dev/null +++ b/tty.h @@ -0,0 +1,7 @@ +#ifndef __TTY_H__ +#define __TTY_H__ + +struct termios; +int tty_open(const char *tty, struct termios *old); + +#endif diff --git a/watch.c b/watch.c new file mode 100644 index 0000000..65755f4 --- /dev/null +++ b/watch.c @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2016-2018, Linaro Ltd. + * All rights reserved. + * + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "cdba.h" +#include "list.h" +#include "watch.h" + +static bool quit_invoked; + +struct watch { + struct list_head node; + + int fd; + int (*cb)(int, void*); + void *data; +}; + +struct timer { + struct list_head node; + struct timeval tv; + + void (*cb)(void *); + void *data; +}; + +static struct list_head read_watches = LIST_INIT(read_watches); +static struct list_head timer_watches = LIST_INIT(timer_watches); + +void watch_add_readfd(int fd, int (*cb)(int, void*), void *data) +{ + struct watch *w; + + w = calloc(1, sizeof(*w)); + w->fd = fd; + w->cb = cb; + w->data = data; + + list_add(&read_watches, &w->node); +} + +void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data) +{ + struct timeval tv_timeout; + struct timeval now; + struct timer *t; + + t = calloc(1, sizeof(*t)); + + gettimeofday(&now, NULL); + + tv_timeout.tv_sec = timeout_ms / 1000; + tv_timeout.tv_usec = (timeout_ms % 1000) * 1000; + + t->cb = cb; + t->data = data; + timeradd(&now, &tv_timeout, &t->tv); + + list_add(&timer_watches, &t->node); +} + +static struct timeval *watch_timer_next(void) +{ + static struct timeval timeout; + struct timeval now; + struct timer *next; + struct timer *t; + + if (list_empty(&timer_watches)) + return NULL; + + next = list_entry_first(&timer_watches, struct timer, node); + + list_for_each_entry(t, &timer_watches, node) { + if (timercmp(&t->tv, &next->tv, <)) + next = t; + } + + gettimeofday(&now, NULL); + timersub(&next->tv, &now, &timeout); + if (timeout.tv_sec < 0) { + timeout.tv_sec = 0; + timeout.tv_usec = 0; + } + + return &timeout; +} + +static void watch_timer_invoke(void) +{ + struct timeval now; + struct timer *tmp; + struct timer *t; + + gettimeofday(&now, NULL); + + list_for_each_entry_safe(t, tmp, &timer_watches, node) { + if (timercmp(&t->tv, &now, <)) { + t->cb(t->data); + + list_del(&t->node); + free(t); + } + } +} + +void watch_quit(void) +{ + quit_invoked = true; +} + +int watch_main_loop(bool (*quit_cb)(void)) +{ + struct timeval *timeoutp; + struct watch *w; + fd_set rfds; + int nfds; + int ret; + + while (!quit_invoked) { + if (quit_cb && quit_cb()) + break; + + nfds = 0; + + list_for_each_entry(w, &read_watches, node) { + nfds = MAX(nfds, w->fd); + FD_SET(w->fd, &rfds); + } + + timeoutp = watch_timer_next(); + ret = select(nfds + 1, &rfds, NULL, NULL, timeoutp); + if (ret < 0 && errno == EINTR) + continue; + else if (ret < 0) { + int err = errno; + fprintf(stderr, "select returned %s\n", strerror(err)); + return -err; + } + + watch_timer_invoke(); + + list_for_each_entry(w, &read_watches, node) { + if (FD_ISSET(w->fd, &rfds)) { + ret = w->cb(w->fd, w->data); + if (ret < 0) { + fprintf(stderr, "cb returned %d\n", ret); + return ret; + } + } + } + } + + return 0; +} + +int watch_run(void) +{ + struct watch *w; + bool found = false; + + list_for_each_entry(w, &read_watches, node) { + if (w->fd == STDIN_FILENO) + found = true; + } + + if (!found) { + fprintf(stderr, "rfds is trash!\n"); + return -EINVAL; + } + + return watch_main_loop(NULL); +} diff --git a/watch.h b/watch.h new file mode 100644 index 0000000..dde554b --- /dev/null +++ b/watch.h @@ -0,0 +1,11 @@ +#ifndef __WATCH_H__ +#define __WATCH_H__ + +void watch_add_readfd(int fd, int (*cb)(int, void*), void *data); +int watch_add_quit(int (*cb)(int, void*), void *data); +void watch_timer_add(int timeout_ms, void (*cb)(void *), void *data); +void watch_quit(void); +int watch_main_loop(bool (*quit_cb)(void)); +int watch_run(void); + +#endif