Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ppps: add support for separate USB-3 PPPS path #36

Merged
merged 1 commit into from
Oct 30, 2023
Merged
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
1 change: 1 addition & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ struct device {
char *serial;
char *description;
char *ppps_path;
char *ppps3_path;
struct list_head *users;
unsigned voltage;
bool tickle_mmc;
Expand Down
2 changes: 2 additions & 0 deletions device_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ static void parse_board(struct device_parser *dp)
dev->usb_always_on = !strcmp(value, "true");
} else if (!strcmp(key, "ppps_path")) {
dev->ppps_path = strdup(value);
} else if (!strcmp(key, "ppps3_path")) {
dev->ppps3_path = strdup(value);
} else {
fprintf(stderr, "device parser: unknown key \"%s\"\n", key);
exit(1);
Expand Down
44 changes: 30 additions & 14 deletions ppps.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,49 @@

#include "device.h"

#define PPPS_BASE_PATH "/sys/bus/usb/devices"
#define PPPS_BASE_PATH_LEN strlen(PPPS_BASE_PATH)
#define PPPS_BASE_PATH "/sys/bus/usb/devices/%s/disable"

void ppps_power(struct device *dev, bool on)
void ppps_power_path(const char *ppps_path, bool on)
{
static char *path = NULL;
int rc, fd;

/* Only need to figure out the whole string once */
if (!path) {
/* ppps_path should be like "2-2:1.0/2-2-port2" */
asprintf(&path, "%s/%s/disable", PPPS_BASE_PATH, dev->ppps_path);
}

// fprintf(stderr, "ppps_power: %-3s %s\n", on ? "on" : "off", path);
//fprintf(stderr, "ppps_power: %-3s %s\n", on ? "on" : "off", path);

fd = open(path, O_WRONLY);
fd = open(ppps_path, O_WRONLY);
if (fd < 0) {
fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno));
fprintf(stderr, "failed to open %s: %s\n", ppps_path, strerror(errno));
if (errno != ENOENT)
fprintf(stderr, "Maybe missing permissions (see https://git.io/JIB2Z)\n");
return;
}

rc = write(fd, on ? "0" : "1", 1);
if (rc < 0)
fprintf(stderr, "failed to write to %s: %s\n", path, strerror(errno));
fprintf(stderr, "failed to write to %s: %s\n", ppps_path, strerror(errno));

close(fd);
}

void ppps_power(struct device *dev, bool on)
{
/* ppps_path should be like "2-2:1.0/2-2-port2" */
if (dev->ppps_path[0] != '/') {
char *temp;

asprintf(&temp, PPPS_BASE_PATH, dev->ppps_path);
free(dev->ppps_path);
dev->ppps_path = temp;
}

if (dev->ppps3_path[0] != '/') {
char *temp;

asprintf(&temp, PPPS_BASE_PATH, dev->ppps3_path);
free(dev->ppps3_path);
dev->ppps3_path = temp;
}

ppps_power_path(dev->ppps_path, on);
if (dev->ppps3_path)
ppps_power_path(dev->ppps3_path, on);
}