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 <bjorn.andersson@linaro.org>
This commit is contained in:
Bjorn Andersson
2021-01-22 18:34:58 -06:00
parent 4913b8b1c7
commit 1abac8b200
10 changed files with 64 additions and 40 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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

View File

@@ -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)

View File

@@ -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);

View File

@@ -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);