2018-11-08 16:15:34 +01:00
|
|
|
#include <windows.h>
|
|
|
|
#include <ntstatus.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-04 23:22:38 +01:00
|
|
|
#include "amex/gpio.h"
|
2018-11-08 16:15:34 +01:00
|
|
|
|
2019-03-04 23:22:38 +01:00
|
|
|
#include "hook/iohook.h"
|
2018-11-08 16:15:34 +01:00
|
|
|
|
2019-05-14 17:15:20 +02:00
|
|
|
#include "hooklib/setupapi.h"
|
|
|
|
|
2018-11-08 16:15:34 +01:00
|
|
|
#include "util/dprintf.h"
|
2019-05-13 22:49:18 +02:00
|
|
|
#include "util/str.h"
|
2018-11-08 16:15:34 +01:00
|
|
|
|
|
|
|
enum {
|
|
|
|
GPIO_IOCTL_SET_LEDS = 0x8000A004,
|
|
|
|
GPIO_IOCTL_GET_PSW = 0x80006008,
|
|
|
|
GPIO_IOCTL_GET_DIPSW = 0x8000600C,
|
|
|
|
GPIO_IOCTL_DESCRIBE = 0x80006014,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
GPIO_TYPE_NONE = 0,
|
|
|
|
GPIO_TYPE_LED = 1,
|
|
|
|
GPIO_TYPE_DIPSW = 2,
|
|
|
|
GPIO_TYPE_PSW = 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
|
|
|
struct gpio_port {
|
|
|
|
uint8_t unknown;
|
|
|
|
|
|
|
|
/* Number of distinct instances of this thing..? */
|
|
|
|
uint8_t count;
|
|
|
|
|
|
|
|
/* Type of GPIO port */
|
|
|
|
uint16_t type;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct gpio_ports {
|
|
|
|
uint8_t unknown; /* Maybe a count of valid items in the array idk */
|
|
|
|
struct gpio_port ports[32];
|
|
|
|
};
|
|
|
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
static HRESULT gpio_handle_irp(struct irp *irp);
|
|
|
|
static HRESULT gpio_handle_open(struct irp *irp);
|
|
|
|
static HRESULT gpio_handle_close(struct irp *irp);
|
|
|
|
static HRESULT gpio_handle_ioctl(struct irp *irp);
|
|
|
|
|
|
|
|
static HRESULT gpio_ioctl_get_psw(struct irp *irp);
|
|
|
|
static HRESULT gpio_ioctl_get_dipsw(struct irp *irp);
|
|
|
|
static HRESULT gpio_ioctl_describe(struct irp *irp);
|
|
|
|
static HRESULT gpio_ioctl_set_leds(struct irp *irp);
|
|
|
|
|
|
|
|
static const struct gpio_ports gpio_ports = {
|
|
|
|
.ports = {
|
|
|
|
{
|
|
|
|
.type = GPIO_TYPE_LED,
|
|
|
|
.count = 2,
|
|
|
|
}, {
|
|
|
|
.type = GPIO_TYPE_DIPSW,
|
|
|
|
.count = 8,
|
|
|
|
}, {
|
|
|
|
.type = GPIO_TYPE_PSW,
|
|
|
|
.count = 2,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2018-11-24 00:22:55 +01:00
|
|
|
static_assert(sizeof(gpio_ports) == 129, "GPIO port map size");
|
2018-11-08 16:15:34 +01:00
|
|
|
|
2019-05-04 19:12:20 +02:00
|
|
|
static HANDLE gpio_fd;
|
|
|
|
static struct gpio_config gpio_config;
|
2019-04-20 23:42:13 +02:00
|
|
|
|
2019-05-04 19:12:20 +02:00
|
|
|
HRESULT gpio_hook_init(const struct gpio_config *cfg)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
2019-04-20 23:42:13 +02:00
|
|
|
|
2019-05-04 19:12:20 +02:00
|
|
|
assert(cfg != NULL);
|
2019-04-20 23:42:13 +02:00
|
|
|
|
2019-05-04 19:12:20 +02:00
|
|
|
if (!cfg->enable) {
|
|
|
|
return S_FALSE;
|
2019-04-20 23:42:13 +02:00
|
|
|
}
|
|
|
|
|
2019-05-04 19:12:20 +02:00
|
|
|
memcpy(&gpio_config, cfg, sizeof(*cfg));
|
|
|
|
|
2019-11-03 17:02:54 +01:00
|
|
|
hr = iohook_open_nul_fd(&gpio_fd);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2019-05-04 19:12:20 +02:00
|
|
|
hr = iohook_push_handler(gpio_handle_irp);
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
hr = setupapi_add_phantom_dev(&gpio_guid, L"$gpio");
|
|
|
|
|
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
2018-11-08 16:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT gpio_handle_irp(struct irp *irp)
|
|
|
|
{
|
|
|
|
assert(irp != NULL);
|
|
|
|
|
|
|
|
if (irp->op != IRP_OP_OPEN && irp->fd != gpio_fd) {
|
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (irp->op) {
|
|
|
|
case IRP_OP_OPEN: return gpio_handle_open(irp);
|
|
|
|
case IRP_OP_CLOSE: return gpio_handle_close(irp);
|
|
|
|
case IRP_OP_IOCTL: return gpio_handle_ioctl(irp);
|
|
|
|
default: return HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT gpio_handle_open(struct irp *irp)
|
|
|
|
{
|
2019-05-13 22:49:18 +02:00
|
|
|
if (!wstr_eq(irp->open_filename, L"$gpio")) {
|
2018-11-08 16:15:34 +01:00
|
|
|
return iohook_invoke_next(irp);
|
|
|
|
}
|
|
|
|
|
|
|
|
dprintf("GPIO: Open device\n");
|
|
|
|
irp->fd = gpio_fd;
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT gpio_handle_close(struct irp *irp)
|
|
|
|
{
|
|
|
|
dprintf("GPIO: Close device\n");
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT gpio_handle_ioctl(struct irp *irp)
|
|
|
|
{
|
|
|
|
switch (irp->ioctl) {
|
|
|
|
case GPIO_IOCTL_SET_LEDS:
|
|
|
|
return gpio_ioctl_set_leds(irp);
|
|
|
|
|
|
|
|
case GPIO_IOCTL_GET_PSW:
|
|
|
|
return gpio_ioctl_get_psw(irp);
|
|
|
|
|
|
|
|
case GPIO_IOCTL_GET_DIPSW:
|
|
|
|
return gpio_ioctl_get_dipsw(irp);
|
|
|
|
|
|
|
|
case GPIO_IOCTL_DESCRIBE:
|
|
|
|
return gpio_ioctl_describe(irp);
|
|
|
|
|
|
|
|
default:
|
2018-12-17 23:34:15 +01:00
|
|
|
dprintf("GPIO: Unknown ioctl %08x, write %i read %i\n",
|
2018-11-08 16:15:34 +01:00
|
|
|
irp->ioctl,
|
2018-12-17 23:34:15 +01:00
|
|
|
(int) irp->write.nbytes,
|
|
|
|
(int) irp->read.nbytes);
|
2018-11-08 16:15:34 +01:00
|
|
|
|
|
|
|
return HRESULT_FROM_WIN32(ERROR_INVALID_FUNCTION);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT gpio_ioctl_get_dipsw(struct irp *irp)
|
|
|
|
{
|
|
|
|
uint32_t dipsw;
|
2019-05-04 19:12:20 +02:00
|
|
|
size_t i;
|
|
|
|
|
|
|
|
dipsw = 0;
|
|
|
|
|
|
|
|
for (i = 0 ; i < 8 ; i++) {
|
|
|
|
if (gpio_config.dipsw[i]) {
|
|
|
|
dipsw |= 1 << i;
|
|
|
|
}
|
|
|
|
}
|
2018-11-08 16:15:34 +01:00
|
|
|
|
|
|
|
//dprintf("GPIO: Get dipsw %08x\n", dipsw);
|
|
|
|
|
|
|
|
return iobuf_write_le32(&irp->read, dipsw);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT gpio_ioctl_get_psw(struct irp *irp)
|
|
|
|
{
|
|
|
|
uint32_t result;
|
|
|
|
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
/* Bit 0 == SW1 == Alt. Test */
|
|
|
|
/* Bit 1 == SW2 == Alt. Service */
|
|
|
|
|
2019-11-17 19:16:50 +01:00
|
|
|
if (GetAsyncKeyState(gpio_config.vk_sw1) & 0x8000) {
|
2019-05-04 19:12:20 +02:00
|
|
|
result |= 1 << 0;
|
|
|
|
}
|
|
|
|
|
2019-11-17 19:16:50 +01:00
|
|
|
if (GetAsyncKeyState(gpio_config.vk_sw2) & 0x8000) {
|
2019-05-04 19:12:20 +02:00
|
|
|
result |= 1 << 1;
|
|
|
|
}
|
|
|
|
|
2018-11-08 16:15:34 +01:00
|
|
|
return iobuf_write_le32(&irp->read, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT gpio_ioctl_describe(struct irp *irp)
|
|
|
|
{
|
2019-10-13 20:48:10 +02:00
|
|
|
HRESULT hr;
|
|
|
|
|
2018-11-08 16:15:34 +01:00
|
|
|
dprintf("GPIO: Describe GPIO ports\n");
|
|
|
|
|
2019-10-13 20:48:10 +02:00
|
|
|
hr = iobuf_write(&irp->read, &gpio_ports, sizeof(gpio_ports));
|
2018-11-08 16:15:34 +01:00
|
|
|
|
2019-10-13 20:48:10 +02:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
dprintf("GPIO: Describe GPIO ports failed: %08x\n", (int) hr);
|
2018-11-08 16:15:34 +01:00
|
|
|
}
|
|
|
|
|
2019-10-13 20:48:10 +02:00
|
|
|
return hr;
|
2018-11-08 16:15:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static HRESULT gpio_ioctl_set_leds(struct irp *irp)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|