Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions cdba-power.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ void cdba_send_buf(int type, size_t len, const void *buf)
static void usage(const char *name)
{
fprintf(stderr, "Usage: %s <board> on|off\n", name);
fprintf(stderr, "Usage: %s all-off\n", name);
exit(EXIT_FAILURE);
}

Expand All @@ -33,22 +34,47 @@ bool ready(void)
return device_is_running(selected_device);
}

static int all_off(void)
{
struct device *next = NULL, *dev;
int ret = 0;

while ((next = device_next(next))) {
dev = device_open_power_only(next->board, "nobody");
if (!dev) {
fprintf(stderr, "failed to open %s\n", next->board);
ret = EXIT_FAILURE;
continue;
}

device_usb(dev, false);
device_power_off(dev);
device_close(dev);
}

return ret;
}

int main(int argc, char **argv)
{
const char *home;
const char *name;
bool all = false;
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
if (argc == 2 && !strcmp(argv[1], "all-off")) {
all = true;
} else if (argc == 3) {
if (!strcmp(argv[2], "on"))
on = true;
else if (!strcmp(argv[2], "off"))
on = false;
else
usage(argv[0]);
} else {
usage(argv[0]);
}

home = getenv("HOME");
if (home)
Expand All @@ -63,6 +89,9 @@ int main(int argc, char **argv)
}
}

if (all)
return all_off();

name = argv[1];
selected_device = device_open(name, "nobody");
if (!selected_device) {
Expand Down
41 changes: 38 additions & 3 deletions device.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ void device_add(struct device *device)
list_append(&devices, &device->node);
}

struct device *device_next(struct device *device)
{
struct list_head *item;

item = device ? device->node.next : devices.next;
if (item == &devices)
return NULL;

return list_entry(item, struct device, node);
}

static void device_lock(struct device *device)
{
char lock[PATH_MAX];
Expand Down Expand Up @@ -103,8 +114,8 @@ static bool device_check_access(struct device *device,
return false;
}

struct device *device_open(const char *board,
const char *username)
static struct device *__device_open(const char *board, const char *username,
bool power_only)
{
struct device *device;

Expand Down Expand Up @@ -133,10 +144,20 @@ struct device *device_open(const char *board,

if (device_has_control(device, open)) {
device->cdb = device_control(device, open);
if (!device->cdb)
if (!device->cdb) {
if (power_only) {
warnx("failed to open device controller for %s",
board);
return NULL;
}
errx(1, "failed to open device controller");
}
}

/* Switching power and USB off needs neither console nor fastboot. */
if (power_only)
return device;

device->console = device_console(device, open);
if (!device->console)
errx(1, "failed to open device console");
Expand All @@ -159,6 +180,20 @@ struct device *device_open(const char *board,
return device;
}

struct device *device_open(const char *board, const char *username)
{
return __device_open(board, username, false);
}

/*
* Open only the board's controller (no console), enough to switch power and
* USB off. Unlike device_open() this doesn't exit if the console is missing.
*/
struct device *device_open_power_only(const char *board, const char *username)
{
return __device_open(board, username, true);
}

static void device_impl_power(struct device *device, bool on)
{
device_control(device, power, on);
Expand Down
3 changes: 3 additions & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ struct device_qdl_user {
};

void device_add(struct device *device);
struct device *device_next(struct device *device);

struct device *device_open(const char *board,
const char *username);
struct device *device_open_power_only(const char *board,
const char *username);
void device_close(struct device *dev);
int device_power_on(struct device *device, enum power_on_mode mode);
int device_power_off(struct device *device);
Expand Down
6 changes: 4 additions & 2 deletions drivers/alpaca.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ static void *alpaca_open(struct device *dev)
alpaca = calloc(1, sizeof(*alpaca));

alpaca->alpaca_fd = tty_open(dev->control_dev, &alpaca->alpaca_tios);
if (alpaca->alpaca_fd < 0)
err(1, "failed to open %s", dev->control_dev);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This way we loose error messages. While there is one usecase where they don't make sense, in general they are useful.

if (alpaca->alpaca_fd < 0) {
free(alpaca);
return NULL;
}

alpaca_device_power(alpaca, 0);

Expand Down
15 changes: 12 additions & 3 deletions drivers/ftdi-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,18 @@ static void *ftdi_gpio_open(struct device *dev)

ret = ftdi_usb_open_string(ftdi_gpio->interface[ftdi_interface],
ftdi_gpio->options->ftdi.description);
if (ret < 0)
errx(1, "failed to open ftdi gpio device '%s' (%d)",
ftdi_gpio->options->ftdi.description, ret);
if (ret < 0) {
warnx("failed to open ftdi gpio device '%s' (%d)",
ftdi_gpio->options->ftdi.description, ret);
for (i = 0; i < FTDI_INTERFACE_COUNT; ++i) {
if (!ftdi_gpio->interface[i])
continue;
ftdi_usb_close(ftdi_gpio->interface[i]);
ftdi_free(ftdi_gpio->interface[i]);
}
free(ftdi_gpio);
return NULL;
}

ftdi_set_bitmode(ftdi_gpio->interface[ftdi_interface],
0xFF, BITMODE_BITBANG);
Expand Down
6 changes: 4 additions & 2 deletions drivers/pic32cx.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ static void *pic32cx_open(struct device *dev)
pic32cx = calloc(1, sizeof(*pic32cx));

pic32cx->fd = tty_open(dev->control_dev, &pic32cx->tios);
if (pic32cx->fd < 0)
err(1, "failed to open %s", dev->control_dev);
if (pic32cx->fd < 0) {
free(pic32cx);
return NULL;
}

pic32cx_device_power(pic32cx, 1);

Expand Down
6 changes: 4 additions & 2 deletions drivers/qcomlt_dbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ static void *qcomlt_dbg_open(struct device *dev)
dbg = calloc(1, sizeof(*dbg));

dbg->fd = tty_open(dev->control_dev, &dbg->orig_tios);
if (dbg->fd < 0)
err(1, "failed to open %s", dev->control_dev);
if (dbg->fd < 0) {
free(dbg);
return NULL;
}

// fprintf(stderr, "qcomlt_dbg_open()\n");
write(dbg->fd, "brpu", 4);
Expand Down
6 changes: 4 additions & 2 deletions tty.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ int tty_open(const char *tty, struct termios *old)
int fd;

fd = open(tty, O_RDWR | O_NOCTTY | O_EXCL);
if (fd < 0)
err(1, "unable to open \"%s\"", tty);
if (fd < 0) {
warn("unable to open \"%s\"", tty);
return -1;
}

ret = tcgetattr(fd, old);
if (ret < 0)
Expand Down
Loading