From 1abac8b20008a0da827cb67b3ab75f608efb95d6 Mon Sep 17 00:00:00 2001 From: Bjorn Andersson Date: Fri, 22 Jan 2021 18:34:58 -0600 Subject: [PATCH] device: Combine power_on/power_off througout Squash the power_on and power_off into a single "power" function, to harmonize the code wrt the USB handling. Signed-off-by: Bjorn Andersson --- alpaca.c | 12 ++++++++++-- alpaca.h | 3 +-- cdb_assist.c | 14 +++++++++++--- cdb_assist.h | 5 ++--- cdba-server.c | 10 +++++----- conmux.c | 10 +++++++++- conmux.h | 3 +-- device.c | 26 +++++++++++++++++--------- device.h | 10 ++++------ device_parser.c | 11 ++++------- 10 files changed, 64 insertions(+), 40 deletions(-) diff --git a/alpaca.c b/alpaca.c index ec13ea8..1b3cf03 100644 --- a/alpaca.c +++ b/alpaca.c @@ -147,7 +147,7 @@ static void alpaca_tick(void *data) } } -int alpaca_power_on(struct device *dev) +static int alpaca_power_on(struct device *dev) { struct alpaca *alpaca = dev->cdb; @@ -157,7 +157,7 @@ int alpaca_power_on(struct device *dev) return 0; } -int alpaca_power_off(struct device *dev) +static int alpaca_power_off(struct device *dev) { alpaca_device_power(dev->cdb, 0); @@ -167,6 +167,14 @@ int alpaca_power_off(struct device *dev) return 0; } +int alpaca_power(struct device *dev, bool on) +{ + if (on) + return alpaca_power_on(dev); + else + return alpaca_power_off(dev); +} + void alpaca_fastboot_key(struct device *dev, bool on) { alpaca_output_bit(dev->cdb, 2, on); diff --git a/alpaca.h b/alpaca.h index 0faa87d..4766d89 100644 --- a/alpaca.h +++ b/alpaca.h @@ -6,8 +6,7 @@ struct alpaca; void *alpaca_open(struct device *dev); -int alpaca_power_on(struct device *dev); -int alpaca_power_off(struct device *dev); +int alpaca_power(struct device *dev, bool on); void alpaca_fastboot_key(struct device *dev, bool on); #endif diff --git a/cdb_assist.c b/cdb_assist.c index 3038f3f..cbb7feb 100644 --- a/cdb_assist.c +++ b/cdb_assist.c @@ -314,7 +314,7 @@ void cdb_vbus(struct cdb_assist *cdb, bool on) cdb_ctrl_write(cdb, &cmd[on], 1); } -int cdb_assist_power_on(struct device *dev) +static int cdb_assist_power_on(struct device *dev) { struct cdb_assist *cdb = dev->cdb; @@ -325,7 +325,7 @@ int cdb_assist_power_on(struct device *dev) return 0; } -int cdb_assist_power_off(struct device *dev) +static int cdb_assist_power_off(struct device *dev) { struct cdb_assist *cdb = dev->cdb; @@ -335,7 +335,15 @@ int cdb_assist_power_off(struct device *dev) return 0; } -void cdb_assist_vbus(struct device *dev, bool on) +int cdb_assist_power(struct device *dev, bool on) +{ + if (on) + return cdb_assist_power_on(dev); + else + return cdb_assist_power_off(dev); +} + +void cdb_assist_usb(struct device *dev, bool on) { cdb_vbus(dev->cdb, on); } diff --git a/cdb_assist.h b/cdb_assist.h index 9269c27..387bb5d 100644 --- a/cdb_assist.h +++ b/cdb_assist.h @@ -10,9 +10,8 @@ struct cdb_assist; void *cdb_assist_open(struct device *dev); void cdb_assist_close(struct cdb_assist *cdb); -int cdb_assist_power_on(struct device *dev); -int cdb_assist_power_off(struct device *dev); -void cdb_assist_vbus(struct device *dev, bool on); +int cdb_assist_power(struct device *dev, bool on); +void cdb_assist_usb(struct device *dev, bool on); void cdb_gpio(struct cdb_assist *cdb, int gpio, bool on); int cdb_target_write(struct device *dev, const void *buf, size_t len); void cdb_send_break(struct device *dev); diff --git a/cdba-server.c b/cdba-server.c index 9062aa7..e71e71a 100644 --- a/cdba-server.c +++ b/cdba-server.c @@ -201,12 +201,12 @@ static int handle_stdin(int fd, void *buf) // fprintf(stderr, "hard reset\n"); break; case MSG_POWER_ON: - device_power_on(selected_device); + device_power(selected_device, true); invoke_reply(MSG_POWER_ON); break; case MSG_POWER_OFF: - device_power_off(selected_device); + device_power(selected_device, false); invoke_reply(MSG_POWER_OFF); break; @@ -220,10 +220,10 @@ static int handle_stdin(int fd, void *buf) device_print_status(selected_device); break; case MSG_VBUS_ON: - device_vbus(selected_device, true); + device_usb(selected_device, true); break; case MSG_VBUS_OFF: - device_vbus(selected_device, false); + device_usb(selected_device, false); break; case MSG_SEND_BREAK: device_send_break(selected_device); @@ -413,7 +413,7 @@ int main(int argc, char **argv) done: - device_power_off(selected_device); + device_power(selected_device, false); return 0; } diff --git a/conmux.c b/conmux.c index 32c40f3..49ef643 100644 --- a/conmux.c +++ b/conmux.c @@ -308,7 +308,7 @@ int conmux_power_on(struct device *dev) return write(conmux->fd, sz, sizeof(sz)); } -int conmux_power_off(struct device *dev) +static int conmux_power_off(struct device *dev) { struct conmux *conmux = dev->cdb; char sz[] = "~$off\n"; @@ -318,6 +318,14 @@ int conmux_power_off(struct device *dev) return write(conmux->fd, sz, sizeof(sz)); } +int conmux_power(struct device *dev, bool on) +{ + if (on) + return conmux_power_on(dev); + else + return conmux_power_off(dev); +} + int conmux_write(struct device *dev, const void *buf, size_t len) { struct conmux *conmux = dev->cdb; diff --git a/conmux.h b/conmux.h index 39ab3f0..b23fa13 100644 --- a/conmux.h +++ b/conmux.h @@ -6,8 +6,7 @@ struct conmux; void *conmux_open(struct device *dev); -int conmux_power_on(struct device *dev); -int conmux_power_off(struct device *dev); +int conmux_power(struct device *dev, bool on); int conmux_write(struct device *dev, const void *buf, size_t len); #endif diff --git a/device.c b/device.c index 376a38a..8edd63f 100644 --- a/device.c +++ b/device.c @@ -117,15 +117,15 @@ static void device_release_fastboot_key(void *data) device->fastboot_key(device, false); } -int device_power_on(struct device *device) +static int device_power_on(struct device *device) { - if (!device || !device->power_on) + if (!device || !device->power) return 0; if (device->fastboot_key_timeout) device->fastboot_key(device, true); - device->power_on(device); + device->power(device, true); if (device->fastboot_key_timeout) watch_timer_add(device->fastboot_key_timeout * 1000, device_release_fastboot_key, device); @@ -133,26 +133,34 @@ int device_power_on(struct device *device) return 0; } -int device_power_off(struct device *device) +static int device_power_off(struct device *device) { - if (!device || !device->power_off) + if (!device || !device->power) return 0; - device->power_off(device); + device->power(device, false); return 0; } +int device_power(struct device *device, bool on) +{ + if (on) + return device_power_on(device); + else + return device_power_off(device); +} + void device_print_status(struct device *device) { if (device->print_status) device->print_status(device); } -void device_vbus(struct device *device, bool enable) +void device_usb(struct device *device, bool on) { - if (device->vbus) - device->vbus(device, enable); + if (device->usb) + device->usb(device, on); } int device_write(struct device *device, const void *buf, size_t len) diff --git a/device.h b/device.h index 55cda8c..a38048d 100644 --- a/device.h +++ b/device.h @@ -24,10 +24,9 @@ struct device { void (*boot)(struct device *); void *(*open)(struct device *dev); - int (*power_on)(struct device *dev); - int (*power_off)(struct device *dev); + int (*power)(struct device *dev, bool on); + void (*usb)(struct device *dev, bool on); void (*print_status)(struct device *dev); - void (*vbus)(struct device *dev, bool on); int (*write)(struct device *dev, const void *buf, size_t len); void (*fastboot_key)(struct device *dev, bool on); void (*send_break)(struct device *dev); @@ -44,11 +43,10 @@ struct device { void device_add(struct device *device); struct device *device_open(const char *board, struct fastboot_ops *fastboot_ops); -int device_power_on(struct device *device); -int device_power_off(struct device *device); +int device_power(struct device *device, bool on); void device_print_status(struct device *device); -void device_vbus(struct device *device, bool enable); +void device_usb(struct device *device, bool on); int device_write(struct device *device, const void *buf, size_t len); void device_boot(struct device *device, const void *data, size_t len); diff --git a/device_parser.c b/device_parser.c index 63db4ad..2a2d760 100644 --- a/device_parser.c +++ b/device_parser.c @@ -98,24 +98,21 @@ static void parse_board(struct device_parser *dp) dev->control_dev = strdup(value); dev->open = cdb_assist_open; - dev->power_on = cdb_assist_power_on; - dev->power_off = cdb_assist_power_off; + dev->power = cdb_assist_power; dev->print_status = cdb_assist_print_status; - dev->vbus = cdb_assist_vbus; + dev->usb = cdb_assist_usb; dev->fastboot_key = cdb_fastboot_key; } else if (!strcmp(key, "conmux")) { dev->control_dev = strdup(value); dev->open = conmux_open; - dev->power_on = conmux_power_on; - dev->power_off = conmux_power_off; + dev->power = conmux_power; dev->write = conmux_write; } else if (!strcmp(key, "alpaca")) { dev->control_dev = strdup(value); dev->open = alpaca_open; - dev->power_on = alpaca_power_on; - dev->power_off = alpaca_power_off; + dev->power = alpaca_power; dev->fastboot_key = alpaca_fastboot_key; } else if (!strcmp(key, "console")) { dev->console_dev = strdup(value);