mirror of
https://github.com/linux-msm/cdba.git
synced 2026-02-25 13:11:56 -08:00
152 lines
2.7 KiB
C
152 lines
2.7 KiB
C
#include <sys/stat.h>
|
|
|
|
#include <err.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include "cdb_assist.h"
|
|
#include "device.h"
|
|
#include "fastboot.h"
|
|
|
|
#define ARRAY_SIZE(x) ((sizeof(x)/sizeof((x)[0])))
|
|
|
|
static void device_fastboot_boot(struct device *device);
|
|
static void device_fastboot_flash_reboot(struct device *device);
|
|
|
|
struct device {
|
|
char *board;
|
|
char *cdb_serial;
|
|
char *name;
|
|
char *serial;
|
|
unsigned voltage;
|
|
bool tickle_mmc;
|
|
bool pshold_shutdown;
|
|
struct fastboot *fastboot;
|
|
|
|
void (*boot)(struct device *);
|
|
|
|
struct cdb_assist *cdb;
|
|
};
|
|
|
|
static struct device devices[] = {
|
|
};
|
|
|
|
struct device *device_open(const char *board,
|
|
struct fastboot_ops *fastboot_ops)
|
|
{
|
|
struct device *device = NULL;
|
|
int i;
|
|
|
|
for (i = 0; i < ARRAY_SIZE(devices); i++) {
|
|
if (strcmp(devices[i].board, board) == 0) {
|
|
device = &devices[i];
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!device)
|
|
return NULL;
|
|
|
|
device->cdb = cdb_assist_open(device->cdb_serial);
|
|
if (!device->cdb)
|
|
errx(1, "failed to open cdb assist");
|
|
|
|
cdb_set_voltage(device->cdb, device->voltage);
|
|
|
|
device->fastboot = fastboot_open(device->serial, fastboot_ops, NULL);
|
|
|
|
return device;
|
|
}
|
|
|
|
int device_power_on(struct device *device)
|
|
{
|
|
if (!device)
|
|
return 0;
|
|
|
|
cdb_power(device->cdb, true);
|
|
cdb_gpio(device->cdb, 0, true);
|
|
usleep(500000);
|
|
cdb_gpio(device->cdb, 0, false);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int device_enter_fastboot(struct device *device)
|
|
{
|
|
cdb_gpio(device->cdb, 1, true);
|
|
cdb_vbus(device->cdb, true);
|
|
cdb_power(device->cdb, true);
|
|
|
|
return 0;
|
|
}
|
|
|
|
int device_power_off(struct device *device)
|
|
{
|
|
if (!device)
|
|
return 0;
|
|
|
|
cdb_vbus(device->cdb, false);
|
|
cdb_power(device->cdb, false);
|
|
|
|
if (device->pshold_shutdown) {
|
|
cdb_gpio(device->cdb, 2, true);
|
|
sleep(2);
|
|
cdb_gpio(device->cdb, 2, false);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
void device_print_status(struct device *device)
|
|
{
|
|
cdb_assist_print_status(device->cdb);
|
|
}
|
|
|
|
void device_vbus(struct device *device, bool enable)
|
|
{
|
|
cdb_vbus(device->cdb, enable);
|
|
}
|
|
|
|
void device_trigger_fastboot(struct device *device, bool enable)
|
|
{
|
|
cdb_gpio(device->cdb, 1, enable);
|
|
}
|
|
|
|
int device_write(struct device *device, const void *buf, size_t len)
|
|
{
|
|
if (!device)
|
|
return 0;
|
|
|
|
return cdb_target_write(device->cdb, buf, len);
|
|
}
|
|
|
|
void device_break(struct device *device)
|
|
{
|
|
cdb_target_break(device->cdb);
|
|
}
|
|
|
|
const char *device_get_serial(struct device *device)
|
|
{
|
|
return device->serial;
|
|
}
|
|
|
|
static void device_fastboot_boot(struct device *device)
|
|
{
|
|
fastboot_boot(device->fastboot);
|
|
}
|
|
|
|
static void device_fastboot_flash_reboot(struct device *device)
|
|
{
|
|
// fastboot_flash(fb, "boot");
|
|
// fastboot_reboot(fb);
|
|
}
|
|
|
|
void device_boot(struct device *device, const void *data, size_t len)
|
|
{
|
|
fastboot_download(device->fastboot, data, len);
|
|
|
|
device->boot(device);
|
|
}
|