2023-09-24 16:38:54 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2023, Linaro Ltd.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
2024-03-16 17:53:43 +02:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2023-09-24 16:38:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#define _GNU_SOURCE /* for asprintf */
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <err.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
|
|
#include "device.h"
|
|
|
|
|
|
2023-10-10 16:09:20 +03:00
|
|
|
#define PPPS_BASE_PATH "/sys/bus/usb/devices/%s/disable"
|
2023-09-24 16:38:54 +01:00
|
|
|
|
2023-10-10 16:09:20 +03:00
|
|
|
void ppps_power_path(const char *ppps_path, bool on)
|
2023-09-24 16:38:54 +01:00
|
|
|
{
|
|
|
|
|
int rc, fd;
|
|
|
|
|
|
2023-10-10 16:09:20 +03:00
|
|
|
//fprintf(stderr, "ppps_power: %-3s %s\n", on ? "on" : "off", path);
|
2023-09-24 16:38:54 +01:00
|
|
|
|
2023-10-10 16:09:20 +03:00
|
|
|
fd = open(ppps_path, O_WRONLY);
|
2023-09-24 16:38:54 +01:00
|
|
|
if (fd < 0) {
|
2023-10-10 16:09:20 +03:00
|
|
|
fprintf(stderr, "failed to open %s: %s\n", ppps_path, strerror(errno));
|
2023-09-24 16:38:54 +01:00
|
|
|
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)
|
2023-10-10 16:09:20 +03:00
|
|
|
fprintf(stderr, "failed to write to %s: %s\n", ppps_path, strerror(errno));
|
2023-09-24 16:38:54 +01:00
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
}
|
2023-10-10 16:09:20 +03:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-09 22:50:53 +00:00
|
|
|
if (dev->ppps3_path && dev->ppps3_path[0] != '/') {
|
2023-10-10 16:09:20 +03:00
|
|
|
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);
|
|
|
|
|
}
|