diff --git a/cdb_assist.c b/cdb_assist.c index 45cad00..4afede6 100644 --- a/cdb_assist.c +++ b/cdb_assist.c @@ -12,6 +12,7 @@ #include #include "bad.h" +#include "cdb_assist.h" struct cdb_assist { char serial[9]; @@ -405,8 +406,9 @@ static int cdb_ctrl_write(struct cdb_assist *cdb, const char *buf, size_t len) return write(cdb->control_tty, buf, len); } -struct cdb_assist *cdb_assist_open(const char *serial) +void *cdb_assist_open(struct device *dev) { + const char *serial = dev->cdb_serial; struct cdb_assist *cdb; int ret; @@ -431,6 +433,8 @@ struct cdb_assist *cdb_assist_open(const char *serial) if (ret < 0) return NULL; + cdb_set_voltage(cdb, dev->voltage); + return cdb; } @@ -448,26 +452,63 @@ void cdb_assist_close(struct cdb_assist *cdb) close(cdb->target_tty); } -void cdb_power(struct cdb_assist *cdb, bool on) +static void cdb_power(struct cdb_assist *cdb, bool on) { const char cmd[] = "pP"; + cdb_ctrl_write(cdb, &cmd[on], 1); } void cdb_vbus(struct cdb_assist *cdb, bool on) { const char cmd[] = "vV"; + cdb_ctrl_write(cdb, &cmd[on], 1); } +int cdb_assist_power_on(struct device *dev) +{ + struct cdb_assist *cdb = dev->cdb; + + cdb_power(cdb, true); + cdb_gpio(cdb, 0, true); + usleep(500000); + cdb_gpio(cdb, 0, false); + + return 0; +} + +int cdb_assist_power_off(struct device *dev) +{ + struct cdb_assist *cdb = dev->cdb; + + cdb_vbus(cdb, false); + cdb_power(cdb, false); + + if (dev->pshold_shutdown) { + cdb_gpio(cdb, 2, true); + sleep(2); + cdb_gpio(cdb, 2, false); + } + + return 0; +} + +void cdb_assist_vbus(struct device *dev, bool on) +{ + cdb_vbus(dev->cdb, on); +} + void cdb_gpio(struct cdb_assist *cdb, int gpio, bool on) { const char *cmd[] = { "aA", "bB", "cC" }; cdb_ctrl_write(cdb, &cmd[gpio][on], 1); } -int cdb_target_write(struct cdb_assist *cdb, const void *buf, size_t len) +int cdb_target_write(struct device *dev, const void *buf, size_t len) { + struct cdb_assist *cdb = dev->cdb; + return write(cdb->target_tty, buf, len); } @@ -476,13 +517,14 @@ void cdb_target_break(struct cdb_assist *cdb) tcsendbreak(cdb->target_tty, 0); } -int cdb_vref(struct cdb_assist *cdb) +unsigned int cdb_vref(struct cdb_assist *cdb) { return cdb->vref; } -void cdb_assist_print_status(struct cdb_assist *cdb) +void cdb_assist_print_status(struct device *dev) { + struct cdb_assist *cdb = dev->cdb; struct msg hdr; char buf[128]; int n; diff --git a/cdb_assist.h b/cdb_assist.h index f45614a..6d33d72 100644 --- a/cdb_assist.h +++ b/cdb_assist.h @@ -3,18 +3,21 @@ #include +#include "device.h" + struct cdb_assist; -struct cdb_assist *cdb_assist_open(const char *serial); +void *cdb_assist_open(struct device *dev); void cdb_assist_close(struct cdb_assist *cdb); -void cdb_power(struct cdb_assist *cdb, bool on); -void cdb_vbus(struct cdb_assist *cdb, bool on); +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); void cdb_gpio(struct cdb_assist *cdb, int gpio, bool on); -int cdb_target_write(struct cdb_assist *cdb, const void *buf, size_t len); +int cdb_target_write(struct device *dev, const void *buf, size_t len); void cdb_target_break(struct cdb_assist *cdb); unsigned int cdb_vref(struct cdb_assist *cdb); -void cdb_assist_print_status(struct cdb_assist *cdb); +void cdb_assist_print_status(struct device *dev); void cdb_set_voltage(struct cdb_assist *cdb, unsigned mV); #endif diff --git a/device.c b/device.c index e17a66d..9b8598b 100644 --- a/device.c +++ b/device.c @@ -1,5 +1,6 @@ #include +#include #include #include #include @@ -15,22 +16,6 @@ static void device_fastboot_boot(struct device *device); static void device_fastboot_flash_reboot(struct device *device); -struct device { - char *board; - char *cdb_serial; - char *name; - char *serial; - unsigned voltage; - bool tickle_mmc; - bool pshold_shutdown; - struct fastboot *fastboot; - - void (*boot)(struct device *); - bool set_active; - - void *cdb; -}; - static struct device devices[] = { }; @@ -50,11 +35,11 @@ struct device *device_open(const char *board, if (!device) return NULL; - device->cdb = cdb_assist_open(device->cdb_serial); - if (!device->cdb) - errx(1, "failed to open cdb assist"); + assert(device->open); - cdb_set_voltage(device->cdb, device->voltage); + device->cdb = device->open(device); + if (!device->cdb) + errx(1, "failed to open device controller"); device->fastboot = fastboot_open(device->serial, fastboot_ops, NULL); @@ -66,10 +51,9 @@ int device_power_on(struct device *device) if (!device) return 0; - cdb_power(device->cdb, true); - cdb_gpio(device->cdb, 0, true); - usleep(500000); - cdb_gpio(device->cdb, 0, false); + assert(device->power_on); + + device->power_on(device); return 0; } @@ -79,26 +63,23 @@ int device_power_off(struct device *device) if (!device) return 0; - cdb_vbus(device->cdb, false); - cdb_power(device->cdb, false); + assert(device->power_off); - if (device->pshold_shutdown) { - cdb_gpio(device->cdb, 2, true); - sleep(2); - cdb_gpio(device->cdb, 2, false); - } + device->power_off(device); return 0; } void device_print_status(struct device *device) { - cdb_assist_print_status(device->cdb); + if (device->print_status) + device->print_status(device); } void device_vbus(struct device *device, bool enable) { - cdb_vbus(device->cdb, enable); + if (device->vbus) + device->vbus(device, enable); } int device_write(struct device *device, const void *buf, size_t len) @@ -106,7 +87,9 @@ int device_write(struct device *device, const void *buf, size_t len) if (!device) return 0; - return cdb_target_write(device->cdb, buf, len); + assert(device->write); + + return device->write(device, buf, len); } static void device_fastboot_boot(struct device *device) diff --git a/device.h b/device.h index e922cca..894c9dc 100644 --- a/device.h +++ b/device.h @@ -1,11 +1,33 @@ #ifndef __DEVICE_H__ #define __DEVICE_H__ -struct device; struct cdb_assist; struct fastboot; struct fastboot_ops; +struct device { + char *board; + char *cdb_serial; + char *name; + char *serial; + unsigned voltage; + bool tickle_mmc; + bool pshold_shutdown; + struct fastboot *fastboot; + + void (*boot)(struct device *); + + void *(*open)(struct device *dev); + int (*power_on)(struct device *dev); + int (*power_off)(struct device *dev); + void (*print_status)(struct device *dev); + void (*vbus)(struct device *dev, bool on); + int (*write)(struct device *dev, const void *buf, size_t len); + bool set_active; + + void *cdb; +}; + 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);