|
45 | 45 |
|
46 | 46 | #include "device.h" |
47 | 47 |
|
48 | | -#define PPPS_BASE_PATH "/sys/bus/usb/devices" |
49 | | -#define PPPS_BASE_PATH_LEN strlen(PPPS_BASE_PATH) |
| 48 | +#define PPPS_BASE_PATH "/sys/bus/usb/devices/%s/disable" |
50 | 49 |
|
51 | | -void ppps_power(struct device *dev, bool on) |
| 50 | +void ppps_power_path(const char *ppps_path, bool on) |
52 | 51 | { |
53 | | - static char *path = NULL; |
54 | 52 | int rc, fd; |
55 | 53 |
|
56 | | - /* Only need to figure out the whole string once */ |
57 | | - if (!path) { |
58 | | - /* ppps_path should be like "2-2:1.0/2-2-port2" */ |
59 | | - asprintf(&path, "%s/%s/disable", PPPS_BASE_PATH, dev->ppps_path); |
60 | | - } |
61 | | - |
62 | | - // fprintf(stderr, "ppps_power: %-3s %s\n", on ? "on" : "off", path); |
| 54 | + //fprintf(stderr, "ppps_power: %-3s %s\n", on ? "on" : "off", path); |
63 | 55 |
|
64 | | - fd = open(path, O_WRONLY); |
| 56 | + fd = open(ppps_path, O_WRONLY); |
65 | 57 | if (fd < 0) { |
66 | | - fprintf(stderr, "failed to open %s: %s\n", path, strerror(errno)); |
| 58 | + fprintf(stderr, "failed to open %s: %s\n", ppps_path, strerror(errno)); |
67 | 59 | if (errno != ENOENT) |
68 | 60 | fprintf(stderr, "Maybe missing permissions (see https://git.io/JIB2Z)\n"); |
69 | 61 | return; |
70 | 62 | } |
71 | 63 |
|
72 | 64 | rc = write(fd, on ? "0" : "1", 1); |
73 | 65 | if (rc < 0) |
74 | | - fprintf(stderr, "failed to write to %s: %s\n", path, strerror(errno)); |
| 66 | + fprintf(stderr, "failed to write to %s: %s\n", ppps_path, strerror(errno)); |
75 | 67 |
|
76 | 68 | close(fd); |
77 | 69 | } |
| 70 | + |
| 71 | +void ppps_power(struct device *dev, bool on) |
| 72 | +{ |
| 73 | + /* ppps_path should be like "2-2:1.0/2-2-port2" */ |
| 74 | + if (dev->ppps_path[0] != '/') { |
| 75 | + char *temp; |
| 76 | + |
| 77 | + asprintf(&temp, PPPS_BASE_PATH, dev->ppps_path); |
| 78 | + free(dev->ppps_path); |
| 79 | + dev->ppps_path = temp; |
| 80 | + } |
| 81 | + |
| 82 | + if (dev->ppps3_path[0] != '/') { |
| 83 | + char *temp; |
| 84 | + |
| 85 | + asprintf(&temp, PPPS_BASE_PATH, dev->ppps3_path); |
| 86 | + free(dev->ppps3_path); |
| 87 | + dev->ppps3_path = temp; |
| 88 | + } |
| 89 | + |
| 90 | + ppps_power_path(dev->ppps_path, on); |
| 91 | + if (dev->ppps3_path) |
| 92 | + ppps_power_path(dev->ppps3_path, on); |
| 93 | +} |
0 commit comments