2018-12-04 23:53:31 -08:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018, Linaro Ltd.
|
|
|
|
|
* All rights reserved.
|
|
|
|
|
*
|
2024-03-16 17:53:43 +02:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2018-12-04 23:53:31 -08:00
|
|
|
*/
|
|
|
|
|
#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 <termios.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2023-10-02 17:33:03 +03:00
|
|
|
#include "device.h"
|
2024-03-12 20:17:22 +02:00
|
|
|
#include "tty.h"
|
2018-12-04 23:53:31 -08:00
|
|
|
|
|
|
|
|
struct alpaca {
|
|
|
|
|
int alpaca_fd;
|
|
|
|
|
|
|
|
|
|
struct termios alpaca_tios;
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-29 23:43:05 -05:00
|
|
|
static int alpaca_device_power(struct alpaca *alpaca, int on);
|
|
|
|
|
static int alpaca_usb_device_power(struct alpaca *alpaca, int on);
|
|
|
|
|
|
2021-12-17 06:29:35 +03:00
|
|
|
static void *alpaca_open(struct device *dev)
|
2018-12-04 23:53:31 -08:00
|
|
|
{
|
|
|
|
|
struct alpaca *alpaca;
|
|
|
|
|
|
2021-01-22 19:29:08 -06:00
|
|
|
dev->has_power_key = true;
|
|
|
|
|
|
2018-12-04 23:53:31 -08:00
|
|
|
alpaca = calloc(1, sizeof(*alpaca));
|
|
|
|
|
|
2021-01-22 18:06:26 -06:00
|
|
|
alpaca->alpaca_fd = tty_open(dev->control_dev, &alpaca->alpaca_tios);
|
2018-12-04 23:53:31 -08:00
|
|
|
if (alpaca->alpaca_fd < 0)
|
2021-01-22 18:06:26 -06:00
|
|
|
err(1, "failed to open %s", dev->control_dev);
|
2018-12-04 23:53:31 -08:00
|
|
|
|
2020-10-29 23:43:05 -05:00
|
|
|
alpaca_device_power(alpaca, 0);
|
2021-01-22 17:26:17 -06:00
|
|
|
|
|
|
|
|
if (dev->usb_always_on)
|
|
|
|
|
alpaca_usb_device_power(alpaca, 1);
|
|
|
|
|
else
|
|
|
|
|
alpaca_usb_device_power(alpaca, 0);
|
2020-10-29 23:43:05 -05:00
|
|
|
|
|
|
|
|
usleep(500000);
|
|
|
|
|
|
2018-12-04 23:53:31 -08:00
|
|
|
return alpaca;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int alpaca_device_power(struct alpaca *alpaca, int on)
|
|
|
|
|
{
|
|
|
|
|
char buf[32];
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
n = sprintf(buf, "devicePower %d\r", !!on);
|
|
|
|
|
|
|
|
|
|
return write(alpaca->alpaca_fd, buf, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int alpaca_usb_device_power(struct alpaca *alpaca, int on)
|
|
|
|
|
{
|
|
|
|
|
char buf[32];
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
n = sprintf(buf, "usbDevicePower %d\r", !!on);
|
|
|
|
|
|
|
|
|
|
return write(alpaca->alpaca_fd, buf, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int alpaca_output_bit(struct alpaca *alpaca, int bit, int value)
|
|
|
|
|
{
|
|
|
|
|
char buf[32];
|
|
|
|
|
int n;
|
|
|
|
|
|
|
|
|
|
n = sprintf(buf, "ttl outputBit %d %d\r", bit, !!value);
|
|
|
|
|
|
|
|
|
|
return write(alpaca->alpaca_fd, buf, n);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 18:34:58 -06:00
|
|
|
static int alpaca_power_on(struct device *dev)
|
2018-12-04 23:53:31 -08:00
|
|
|
{
|
2021-01-22 19:29:08 -06:00
|
|
|
alpaca_device_power(dev->cdb, 1);
|
2018-12-04 23:53:31 -08:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-22 18:34:58 -06:00
|
|
|
static int alpaca_power_off(struct device *dev)
|
2018-12-04 23:53:31 -08:00
|
|
|
{
|
|
|
|
|
alpaca_device_power(dev->cdb, 0);
|
2021-01-22 17:26:17 -06:00
|
|
|
|
2018-12-04 23:53:31 -08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-10-19 09:55:41 -07:00
|
|
|
|
2021-12-17 06:29:35 +03:00
|
|
|
static int alpaca_power(struct device *dev, bool on)
|
2021-01-22 18:34:58 -06:00
|
|
|
{
|
|
|
|
|
if (on)
|
|
|
|
|
return alpaca_power_on(dev);
|
|
|
|
|
else
|
|
|
|
|
return alpaca_power_off(dev);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 06:29:35 +03:00
|
|
|
static void alpaca_usb(struct device *dev, bool on)
|
2021-01-22 19:29:08 -06:00
|
|
|
{
|
|
|
|
|
struct alpaca *alpaca = dev->cdb;
|
|
|
|
|
|
|
|
|
|
alpaca_usb_device_power(alpaca, on);
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 06:29:35 +03:00
|
|
|
static void alpaca_key(struct device *dev, int key, bool asserted)
|
2021-01-22 19:29:08 -06:00
|
|
|
{
|
|
|
|
|
switch (key) {
|
|
|
|
|
case DEVICE_KEY_FASTBOOT:
|
|
|
|
|
alpaca_output_bit(dev->cdb, 2, asserted);
|
|
|
|
|
break;
|
|
|
|
|
case DEVICE_KEY_POWER:
|
|
|
|
|
alpaca_output_bit(dev->cdb, 1, asserted);
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-10-19 09:55:41 -07:00
|
|
|
}
|
2023-10-02 17:33:03 +03:00
|
|
|
|
|
|
|
|
const struct control_ops alpaca_ops = {
|
|
|
|
|
.open = alpaca_open,
|
|
|
|
|
.power = alpaca_power,
|
|
|
|
|
.usb = alpaca_usb,
|
|
|
|
|
.key = alpaca_key,
|
|
|
|
|
};
|