|
| 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 | +}; |
0 commit comments