chore: Apply code formatting on entire codebase for consistent style
This commit is contained in:
+47
-24
@@ -25,29 +25,37 @@ static bool p4io_transfer(
|
||||
const void *req_payload,
|
||||
size_t req_payload_len,
|
||||
void *resp_payload,
|
||||
size_t resp_payload_len
|
||||
) {
|
||||
size_t resp_payload_len)
|
||||
{
|
||||
size_t transferred_response_payload = resp_payload_len;
|
||||
bool ret = p4io_usb_transfer(ctx->bulk_handle, cmd, ctx->seq_no,
|
||||
req_payload, req_payload_len,
|
||||
resp_payload, &transferred_response_payload);
|
||||
bool ret = p4io_usb_transfer(
|
||||
ctx->bulk_handle,
|
||||
cmd,
|
||||
ctx->seq_no,
|
||||
req_payload,
|
||||
req_payload_len,
|
||||
resp_payload,
|
||||
&transferred_response_payload);
|
||||
|
||||
ctx->seq_no++;
|
||||
|
||||
if(!ret) {
|
||||
if (!ret) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if(resp_payload_len && transferred_response_payload != resp_payload_len) {
|
||||
log_warning("Asked for %u bytes got %u", (unsigned)resp_payload_len,
|
||||
(unsigned)transferred_response_payload);
|
||||
if (resp_payload_len && transferred_response_payload != resp_payload_len) {
|
||||
log_warning(
|
||||
"Asked for %u bytes got %u",
|
||||
(unsigned) resp_payload_len,
|
||||
(unsigned) transferred_response_payload);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
struct p4iodrv_ctx *p4iodrv_open(void) {
|
||||
struct p4iodrv_ctx *p4iodrv_open(void)
|
||||
{
|
||||
struct p4iodrv_ctx *ctx = xcalloc(sizeof(struct p4iodrv_ctx));
|
||||
|
||||
// jamma is read as fast as possible in its own thread, so having them be
|
||||
@@ -57,14 +65,15 @@ struct p4iodrv_ctx *p4iodrv_open(void) {
|
||||
ctx->bulk_handle = p4io_usb_open();
|
||||
ctx->seq_no = 0;
|
||||
|
||||
if(ctx->jamma_handle == INVALID_HANDLE_VALUE || ctx->bulk_handle == INVALID_HANDLE_VALUE) {
|
||||
if (ctx->jamma_handle == INVALID_HANDLE_VALUE ||
|
||||
ctx->bulk_handle == INVALID_HANDLE_VALUE) {
|
||||
free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
p4io_cmd_init(ctx);
|
||||
|
||||
if(!p4io_print_version(ctx)) {
|
||||
if (!p4io_print_version(ctx)) {
|
||||
free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
@@ -72,18 +81,21 @@ struct p4iodrv_ctx *p4iodrv_open(void) {
|
||||
return ctx;
|
||||
}
|
||||
|
||||
void p4iodrv_close(struct p4iodrv_ctx *ctx) {
|
||||
void p4iodrv_close(struct p4iodrv_ctx *ctx)
|
||||
{
|
||||
p4io_usb_close(ctx->jamma_handle);
|
||||
p4io_usb_close(ctx->bulk_handle);
|
||||
free(ctx);
|
||||
}
|
||||
|
||||
bool p4iodrv_read_jamma(struct p4iodrv_ctx *ctx, uint32_t jamma[4]) {
|
||||
bool p4iodrv_read_jamma(struct p4iodrv_ctx *ctx, uint32_t jamma[4])
|
||||
{
|
||||
return p4io_usb_read_jamma(ctx->jamma_handle, jamma);
|
||||
}
|
||||
|
||||
// send something you don't expect a response for
|
||||
static bool p4io_send(struct p4iodrv_ctx *ctx, uint8_t cmd) {
|
||||
static bool p4io_send(struct p4iodrv_ctx *ctx, uint8_t cmd)
|
||||
{
|
||||
return p4io_transfer(ctx, cmd, NULL, 0, NULL, 0);
|
||||
}
|
||||
|
||||
@@ -91,19 +103,21 @@ static bool p4io_send(struct p4iodrv_ctx *ctx, uint8_t cmd) {
|
||||
// Even if we were to check the return value here, the reset command returns a 0
|
||||
// byte response - not even an "OK" status, 0 bytes in total. That technically
|
||||
// should be raising an error, but because reset is a special case it's ignored
|
||||
static void p4io_cmd_init(struct p4iodrv_ctx *ctx) {
|
||||
static void p4io_cmd_init(struct p4iodrv_ctx *ctx)
|
||||
{
|
||||
p4io_send(ctx, P4IO_CMD_INIT);
|
||||
}
|
||||
|
||||
static bool p4io_print_version(struct p4iodrv_ctx *ctx) {
|
||||
static bool p4io_print_version(struct p4iodrv_ctx *ctx)
|
||||
{
|
||||
char p4io_name[128];
|
||||
|
||||
if(!p4io_usb_read_device_name(ctx->bulk_handle, p4io_name)) {
|
||||
if (!p4io_usb_read_device_name(ctx->bulk_handle, p4io_name)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
struct p4io_resp_device_info dev_info;
|
||||
if(!p4iodrv_cmd_device_info(ctx, &dev_info)) {
|
||||
if (!p4iodrv_cmd_device_info(ctx, &dev_info)) {
|
||||
log_warning("p4io get_device_info failed");
|
||||
return false;
|
||||
}
|
||||
@@ -113,21 +127,30 @@ static bool p4io_print_version(struct p4iodrv_ctx *ctx) {
|
||||
|
||||
log_info("p4io name: %s", p4io_name);
|
||||
log_info("p4io type: %08X", dev_info.type);
|
||||
log_info("p4io version: %d.%d.%d", dev_info.version_major, dev_info.version_minor, dev_info.version_revision);
|
||||
log_info(
|
||||
"p4io version: %d.%d.%d",
|
||||
dev_info.version_major,
|
||||
dev_info.version_minor,
|
||||
dev_info.version_revision);
|
||||
log_info("p4io product: %.4s", dev_info.product_code);
|
||||
log_info("p4io build date: %.16s", dev_info.build_date);
|
||||
log_info("p4io build time: %.16s", dev_info.build_time);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p4iodrv_cmd_device_info(struct p4iodrv_ctx *ctx, struct p4io_resp_device_info *info) {
|
||||
return p4io_transfer(ctx, P4IO_CMD_GET_DEVICE_INFO, NULL, 0, info, sizeof(*info));
|
||||
bool p4iodrv_cmd_device_info(
|
||||
struct p4iodrv_ctx *ctx, struct p4io_resp_device_info *info)
|
||||
{
|
||||
return p4io_transfer(
|
||||
ctx, P4IO_CMD_GET_DEVICE_INFO, NULL, 0, info, sizeof(*info));
|
||||
}
|
||||
|
||||
bool p4iodrv_cmd_portout(struct p4iodrv_ctx *ctx, const uint8_t buffer[16]) {
|
||||
bool p4iodrv_cmd_portout(struct p4iodrv_ctx *ctx, const uint8_t buffer[16])
|
||||
{
|
||||
return p4io_transfer(ctx, P4IO_CMD_SET_PORTOUT, buffer, 16, NULL, 0);
|
||||
}
|
||||
|
||||
bool p4iodrv_cmd_coinstock(struct p4iodrv_ctx *ctx, const uint8_t buffer[4]) {
|
||||
bool p4iodrv_cmd_coinstock(struct p4iodrv_ctx *ctx, const uint8_t buffer[4])
|
||||
{
|
||||
return p4io_transfer(ctx, P4IO_CMD_COINSTOCK, buffer, 4, NULL, 0);
|
||||
}
|
||||
|
||||
@@ -10,7 +10,8 @@ struct p4iodrv_ctx;
|
||||
|
||||
struct p4iodrv_ctx *p4iodrv_open(void);
|
||||
void p4iodrv_close(struct p4iodrv_ctx *ctx);
|
||||
bool p4iodrv_cmd_device_info(struct p4iodrv_ctx *ctx, struct p4io_resp_device_info *info);
|
||||
bool p4iodrv_cmd_device_info(
|
||||
struct p4iodrv_ctx *ctx, struct p4io_resp_device_info *info);
|
||||
bool p4iodrv_cmd_portout(struct p4iodrv_ctx *ctx, const uint8_t buffer[16]);
|
||||
bool p4iodrv_cmd_coinstock(struct p4iodrv_ctx *ctx, const uint8_t buffer[4]);
|
||||
bool p4iodrv_read_jamma(struct p4iodrv_ctx *ctx, uint32_t jamma[4]);
|
||||
|
||||
+73
-31
@@ -1,8 +1,8 @@
|
||||
#define LOG_MODULE "p4iodrv-usb"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// clang-format off
|
||||
// Don't format because the order is important here
|
||||
@@ -19,34 +19,47 @@
|
||||
#include "util/log.h"
|
||||
#include "util/str.h"
|
||||
|
||||
HANDLE p4io_usb_open(void) {
|
||||
HANDLE p4io_usb_open(void)
|
||||
{
|
||||
HANDLE handle = INVALID_HANDLE_VALUE;
|
||||
wchar_t p4io_filename[MAX_PATH]; // game uses 1024, but it shouldn't be that long
|
||||
wchar_t p4io_filename[MAX_PATH]; // game uses 1024, but it shouldn't be that
|
||||
// long
|
||||
PSP_DEVICE_INTERFACE_DETAIL_DATA_W detail_data = NULL;
|
||||
HDEVINFO dev_info_set;
|
||||
|
||||
dev_info_set = SetupDiGetClassDevsW(&p4io_guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
|
||||
dev_info_set = SetupDiGetClassDevsW(
|
||||
&p4io_guid, NULL, NULL, DIGCF_DEVICEINTERFACE | DIGCF_PRESENT);
|
||||
|
||||
if(dev_info_set == INVALID_HANDLE_VALUE) {
|
||||
log_warning("SetupDiGetClassDevs fail, is p4io device connected and driver installed?");
|
||||
if (dev_info_set == INVALID_HANDLE_VALUE) {
|
||||
log_warning(
|
||||
"SetupDiGetClassDevs fail, is p4io device connected and driver "
|
||||
"installed?");
|
||||
return INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
SP_DEVICE_INTERFACE_DATA interface_data;
|
||||
interface_data.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
|
||||
|
||||
if(!SetupDiEnumDeviceInterfaces(dev_info_set, NULL, &p4io_guid, 0, &interface_data)) {
|
||||
if (!SetupDiEnumDeviceInterfaces(
|
||||
dev_info_set, NULL, &p4io_guid, 0, &interface_data)) {
|
||||
log_warning("SetupDiEnumDeviceInterfaces fail");
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
DWORD required_size;
|
||||
SetupDiGetDeviceInterfaceDetailW(dev_info_set, &interface_data, NULL, 0, &required_size, NULL);
|
||||
SetupDiGetDeviceInterfaceDetailW(
|
||||
dev_info_set, &interface_data, NULL, 0, &required_size, NULL);
|
||||
|
||||
detail_data = malloc(required_size);
|
||||
detail_data->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA_W);
|
||||
|
||||
if(!SetupDiGetDeviceInterfaceDetailW(dev_info_set, &interface_data, detail_data, required_size, NULL, NULL)) {
|
||||
if (!SetupDiGetDeviceInterfaceDetailW(
|
||||
dev_info_set,
|
||||
&interface_data,
|
||||
detail_data,
|
||||
required_size,
|
||||
NULL,
|
||||
NULL)) {
|
||||
log_warning("SetupDiGetDeviceInterfaceDetailW fail");
|
||||
goto CLEANUP;
|
||||
}
|
||||
@@ -56,28 +69,45 @@ HANDLE p4io_usb_open(void) {
|
||||
|
||||
log_info("p4io found at path %ls", p4io_filename);
|
||||
|
||||
handle = CreateFileW(p4io_filename, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
|
||||
if(handle == INVALID_HANDLE_VALUE) {
|
||||
handle = CreateFileW(
|
||||
p4io_filename,
|
||||
GENERIC_READ | GENERIC_WRITE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
0,
|
||||
NULL);
|
||||
if (handle == INVALID_HANDLE_VALUE) {
|
||||
log_warning("CreateFileW fail");
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
CLEANUP:
|
||||
CLEANUP:
|
||||
free(detail_data);
|
||||
SetupDiDestroyDeviceInfoList(dev_info_set);
|
||||
|
||||
return handle;
|
||||
}
|
||||
void p4io_usb_close(HANDLE p4io_handle) {
|
||||
if(p4io_handle != INVALID_HANDLE_VALUE) {
|
||||
void p4io_usb_close(HANDLE p4io_handle)
|
||||
{
|
||||
if (p4io_handle != INVALID_HANDLE_VALUE) {
|
||||
CloseHandle(p4io_handle);
|
||||
}
|
||||
p4io_handle = INVALID_HANDLE_VALUE;
|
||||
}
|
||||
|
||||
bool p4io_usb_read_jamma(HANDLE interrupt_handle, uint32_t jamma[4]) {
|
||||
bool p4io_usb_read_jamma(HANDLE interrupt_handle, uint32_t jamma[4])
|
||||
{
|
||||
DWORD bytes_returned;
|
||||
if(!DeviceIoControl(interrupt_handle, P4IO_IOCTL_READ_JAMMA_2, NULL, 0, jamma, sizeof(uint32_t[4]), &bytes_returned, NULL)) {
|
||||
if (!DeviceIoControl(
|
||||
interrupt_handle,
|
||||
P4IO_IOCTL_READ_JAMMA_2,
|
||||
NULL,
|
||||
0,
|
||||
jamma,
|
||||
sizeof(uint32_t[4]),
|
||||
&bytes_returned,
|
||||
NULL)) {
|
||||
log_warning("jamma read failed");
|
||||
return false;
|
||||
}
|
||||
@@ -85,9 +115,18 @@ bool p4io_usb_read_jamma(HANDLE interrupt_handle, uint32_t jamma[4]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool p4io_usb_read_device_name(HANDLE bulk_handle, char name[128]) {
|
||||
bool p4io_usb_read_device_name(HANDLE bulk_handle, char name[128])
|
||||
{
|
||||
DWORD bytes_returned;
|
||||
if(!DeviceIoControl(bulk_handle, P4IO_IOCTL_GET_DEVICE_NAME, NULL, 0, name, 128, &bytes_returned, NULL)) {
|
||||
if (!DeviceIoControl(
|
||||
bulk_handle,
|
||||
P4IO_IOCTL_GET_DEVICE_NAME,
|
||||
NULL,
|
||||
0,
|
||||
name,
|
||||
128,
|
||||
&bytes_returned,
|
||||
NULL)) {
|
||||
log_warning("p4io does not support get_name cmd");
|
||||
return false;
|
||||
}
|
||||
@@ -102,18 +141,18 @@ bool p4io_usb_transfer(
|
||||
const void *req_payload,
|
||||
size_t req_payload_len,
|
||||
void *resp_payload,
|
||||
size_t *resp_payload_len
|
||||
) {
|
||||
size_t *resp_payload_len)
|
||||
{
|
||||
DWORD bytes_requested;
|
||||
DWORD bytes_xferred;
|
||||
struct p4io_cmd_package cmd_buf;
|
||||
|
||||
if(bulk_handle == INVALID_HANDLE_VALUE) {
|
||||
if (bulk_handle == INVALID_HANDLE_VALUE) {
|
||||
log_warning("p4io not open");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(req_payload_len > sizeof(cmd_buf.payload)) {
|
||||
if (req_payload_len > sizeof(cmd_buf.payload)) {
|
||||
log_warning("request too big");
|
||||
return false;
|
||||
}
|
||||
@@ -126,32 +165,35 @@ bool p4io_usb_transfer(
|
||||
|
||||
bytes_requested = P4IO_CMD_HEADER_LEN + req_payload_len;
|
||||
|
||||
if(!WriteFile(bulk_handle, &cmd_buf, bytes_requested, &bytes_xferred, 0)) {
|
||||
if (!WriteFile(bulk_handle, &cmd_buf, bytes_requested, &bytes_xferred, 0)) {
|
||||
log_warning("WriteFile failed");
|
||||
return false;
|
||||
}
|
||||
if(bytes_xferred != bytes_requested) {
|
||||
if (bytes_xferred != bytes_requested) {
|
||||
log_warning("WriteFile didn't finish");
|
||||
return false;
|
||||
}
|
||||
|
||||
// must be 65 bytes or requests can stall - only 64 will ever be returned
|
||||
if(!ReadFile(bulk_handle, &cmd_buf, 65, &bytes_xferred, 0)) {
|
||||
log_warning("ReadFile (%u) failed", (unsigned)bytes_requested);
|
||||
if (!ReadFile(bulk_handle, &cmd_buf, 65, &bytes_xferred, 0)) {
|
||||
log_warning("ReadFile (%u) failed", (unsigned) bytes_requested);
|
||||
return false;
|
||||
}
|
||||
|
||||
if(bytes_xferred >= sizeof(struct p4io_cmd_header)) {
|
||||
if (bytes_xferred >= sizeof(struct p4io_cmd_header)) {
|
||||
memcpy(resp_payload, cmd_buf.payload, *resp_payload_len);
|
||||
*resp_payload_len = cmd_buf.header.payload_len;
|
||||
|
||||
if(cmd_buf.header.sof != P4IO_SOF) {
|
||||
if (cmd_buf.header.sof != P4IO_SOF) {
|
||||
log_warning("Response bad header");
|
||||
return false;
|
||||
}
|
||||
|
||||
if(seq_no != cmd_buf.header.seq_num) {
|
||||
log_warning("seq_num mismatch (ours %d != theirs %d)", seq_no, cmd_buf.header.seq_num);
|
||||
if (seq_no != cmd_buf.header.seq_num) {
|
||||
log_warning(
|
||||
"seq_num mismatch (ours %d != theirs %d)",
|
||||
seq_no,
|
||||
cmd_buf.header.seq_num);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -16,7 +16,6 @@ bool p4io_usb_transfer(
|
||||
const void *req_payload,
|
||||
size_t req_payload_len,
|
||||
void *resp_payload,
|
||||
size_t *resp_payload_len
|
||||
);
|
||||
size_t *resp_payload_len);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user